如何解析 DateTime 并将其转换为 RFC 3339 日期时间格式?

How do I parse and convert a DateTime to the RFC 3339 date-time format?

提问人:Oppositional 提问时间:8/20/2008 最后编辑:Mark AmeryOppositional 更新时间:12/6/2022 访问量:54787

问:

如何将 DateTime 结构转换为其等效的 RFC 3339 格式的字符串表示形式和/或将此字符串表示形式解析回 DateTime 结构?RFC-3339 日期时间格式用于许多规范,例如 Atom Syndication 格式

.NET 日期时间 RFC3339 原子馈送

评论


答:

22赞 Oppositional 8/20/2008 #1

这是 C# 中关于如何解析 DateTime 并将其转换为其 RFC-3339 表示形式的实现。它的唯一限制是 DateTime 采用协调世界时 (UTC)。

using System;
using System.Globalization;

namespace DateTimeConsoleApplication
{
    /// <summary>
    /// Provides methods for converting <see cref="DateTime"/> structures to and from the equivalent RFC 3339 string representation.
    /// </summary>
    public static class Rfc3339DateTime
    {
        //============================================================
        //  Private members
        //============================================================
        #region Private Members
        /// <summary>
        /// Private member to hold array of formats that RFC 3339 date-time representations conform to.
        /// </summary>
        private static string[] formats = new string[0];
        /// <summary>
        /// Private member to hold the DateTime format string for representing a DateTime in the RFC 3339 format.
        /// </summary>
        private const string format = "yyyy-MM-dd'T'HH:mm:ss.fffK";
        #endregion

        //============================================================
        //  Public Properties
        //============================================================
        #region Rfc3339DateTimeFormat
        /// <summary>
        /// Gets the custom format specifier that may be used to represent a <see cref="DateTime"/> in the RFC 3339 format.
        /// </summary>
        /// <value>A <i>DateTime format string</i> that may be used to represent a <see cref="DateTime"/> in the RFC 3339 format.</value>
        /// <remarks>
        /// <para>
        /// This method returns a string representation of a <see cref="DateTime"/> that 
        /// is precise to the three most significant digits of the seconds fraction; that is, it represents 
        /// the milliseconds in a date and time value. The <see cref="Rfc3339DateTimeFormat"/> is a valid 
        /// date-time format string for use in the <see cref="DateTime.ToString(String, IFormatProvider)"/> method.
        /// </para>
        /// </remarks>
        public static string Rfc3339DateTimeFormat
        {
            get
            {
                return format;
            }
        }
        #endregion

        #region Rfc3339DateTimePatterns
        /// <summary>
        /// Gets an array of the expected formats for RFC 3339 date-time string representations.
        /// </summary>
        /// <value>
        /// An array of the expected formats for RFC 3339 date-time string representations 
        /// that may used in the <see cref="DateTime.TryParseExact(String, string[], IFormatProvider, DateTimeStyles, out DateTime)"/> method.
        /// </value>
        public static string[] Rfc3339DateTimePatterns
        {
            get
            {
                if (formats.Length > 0)
                {
                    return formats;
                }
                else
                {
                    formats = new string[11];

                    // Rfc3339DateTimePatterns
                    formats[0] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK";
                    formats[1] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffffK";
                    formats[2] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffK";
                    formats[3] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffK";
                    formats[4] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK";
                    formats[5] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffK";
                    formats[6] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fK";
                    formats[7] = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK";

                    // Fall back patterns
                    formats[8] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"; // RoundtripDateTimePattern
                    formats[9] = DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern;
                    formats[10] = DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern;

                    return formats;
                }
            }
        }
        #endregion

        //============================================================
        //  Public Methods
        //============================================================
        #region Parse(string s)
        /// <summary>
        /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent.
        /// </summary>
        /// <param name="s">A string containing a date and time to convert.</param>
        /// <returns>A <see cref="DateTime"/> equivalent to the date and time contained in <paramref name="s"/>.</returns>
        /// <remarks>
        /// The string <paramref name="s"/> is parsed using formatting information in the <see cref="DateTimeFormatInfo.InvariantInfo"/> object.
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="s"/> is a <b>null</b> reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException"><paramref name="s"/> does not contain a valid RFC 3339 string representation of a date and time.</exception>
        public static DateTime Parse(string s)
        {
            //------------------------------------------------------------
            //  Validate parameter
            //------------------------------------------------------------
            if(s == null)
            {
                throw new ArgumentNullException("s");
            }

            DateTime result;
            if (Rfc3339DateTime.TryParse(s, out result))
            {
                return result;
            }
            else
            {
                throw new FormatException(String.Format(null, "{0} is not a valid RFC 3339 string representation of a date and time.", s));
            }
        }
        #endregion

        #region ToString(DateTime utcDateTime)
        /// <summary>
        /// Converts the value of the specified <see cref="DateTime"/> object to its equivalent string representation.
        /// </summary>
        /// <param name="utcDateTime">The Coordinated Universal Time (UTC) <see cref="DateTime"/> to convert.</param>
        /// <returns>A RFC 3339 string representation of the value of the <paramref name="utcDateTime"/>.</returns>
        /// <remarks>
        /// <para>
        /// This method returns a string representation of the <paramref name="utcDateTime"/> that 
        /// is precise to the three most significant digits of the seconds fraction; that is, it represents 
        /// the milliseconds in a date and time value.
        /// </para>
        /// <para>
        /// While it is possible to display higher precision fractions of a second component of a time value, 
        /// that value may not be meaningful. The precision of date and time values depends on the resolution 
        /// of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's 
        /// resolution is approximately 10-15 milliseconds.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentException">The specified <paramref name="utcDateTime"/> object does not represent a <see cref="DateTimeKind.Utc">Coordinated Universal Time (UTC)</see> value.</exception>
        public static string ToString(DateTime utcDateTime)
        {
            if (utcDateTime.Kind != DateTimeKind.Utc)
            {
                throw new ArgumentException("utcDateTime");
            }

            return utcDateTime.ToString(Rfc3339DateTime.Rfc3339DateTimeFormat, DateTimeFormatInfo.InvariantInfo);
        }
        #endregion

        #region TryParse(string s, out DateTime result)
        /// <summary>
        /// Converts the specified string representation of a date and time to its <see cref="DateTime"/> equivalent.
        /// </summary>
        /// <param name="s">A string containing a date and time to convert.</param>
        /// <param name="result">
        /// When this method returns, contains the <see cref="DateTime"/> value equivalent to the date and time 
        /// contained in <paramref name="s"/>, if the conversion succeeded, 
        /// or <see cref="DateTime.MinValue">MinValue</see> if the conversion failed. 
        /// The conversion fails if the s parameter is a <b>null</b> reference (Nothing in Visual Basic), 
        /// or does not contain a valid string representation of a date and time. 
        /// This parameter is passed uninitialized.
        /// </param>
        /// <returns><b>true</b> if the <paramref name="s"/> parameter was converted successfully; otherwise, <b>false</b>.</returns>
        /// <remarks>
        /// The string <paramref name="s"/> is parsed using formatting information in the <see cref="DateTimeFormatInfo.InvariantInfo"/> object.
        /// </remarks>
        public static bool TryParse(string s, out DateTime result)
        {
            //------------------------------------------------------------
            //  Attempt to convert string representation
            //------------------------------------------------------------
            bool wasConverted   = false;
            result              = DateTime.MinValue;

            if (!String.IsNullOrEmpty(s))
            {
                DateTime parseResult;
                if (DateTime.TryParseExact(s, Rfc3339DateTime.Rfc3339DateTimePatterns, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out parseResult))
                {
                    result          = DateTime.SpecifyKind(parseResult, DateTimeKind.Utc);
                    wasConverted    = true;
                }
            }

            return wasConverted;
        }
        #endregion
    }
}

评论

6赞 Matt Howells 4/1/2010
为什么要为BCL中已有的内容编写此代码?
3赞 Bellarmine Head 3/6/2012
在这里必须同意马特的观点。RFC3339不是由 DateTime.ToString 标准格式之一来满足的 - 这令人惊讶 - 但 XmlConvert 可以完成这项工作。例如,我使用XmlConvert.ToString(value, XmlDateTimeSerializationMode.Utc)
0赞 Stoyan Dimov 5/4/2015
嘿,@AndrewWebb,如果数据格式像这样怎么办。我听说这也被认为是RFC3339但不会将其识别为有效的 DateTime,没有任何2015-05-04T10:08:15+00:00RFC3339XmlDateTimeSerializationMode
1赞 Bellarmine Head 5/5/2015
@StoyanDimov:对我有用。var d = System.Xml.XmlConvert.ToDateTime("2015-05-04T10:08:15+00:00", System.Xml.XmlDateTimeSerializationMode.Utc);
4赞 wonea 10/21/2016
感谢这一点,如果你使用的是 .NET Core XmlConvert 不可用。
-3赞 Mark Cidade 8/20/2008 #2

在 .NET 中(假设 UTC):

 datetime.ToString("yyyy-MM-dd'T'HH:mm:ssZ")
 

DateTime.Parse()可用于转换回结构。DateTime

评论

1赞 Bellarmine Head 3/2/2012
不要以为“YYYY-MM-DD'T'HH:mm:ss”可能是对的;它没有与 UTC 的偏移量,也没有与 'Z' 的偏移量(= 与 UTC 的零偏移量)。
3赞 Tomáš Linhart 6/9/2016
这对我不起作用。我不得不使用datetime.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ssZ")
4赞 Mark Cidade 6/9/2016
仅当 DateTime 处于 UTC 时区(世界时)时,它才有效。
1赞 Aris 2/20/2021
必须是 yyyy 和 dd,而不是大写字母
64赞 Matt Howells 9/18/2008 #3

您无需编写自己的转换代码。只需使用

XmlConvert.ToDateTime(string s, XmlDateTimeSerializationMode dateTimeOption)

解析 RFC-3339 字符串,以及

XmlConvert.ToString(DateTime value, XmlDateTimeSerializationMode dateTimeOption)

将 (UTC) 日期时间转换为字符串。

参考文献
http://msdn.microsoft.com/en-us/library/ms162342(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/ms162344(v=vs.110).aspx

评论

6赞 Yiannis Mpourkelis 12/4/2014
补充一点,这个库可以很好地解析和转换谷歌日历事件中的日期到/从 .NET 日期时间转换
-6赞 user1560963 4/12/2017 #4

一个简单的方程式将能够获得您想要的结果:

rfcFormat = DateDiff("s", "1/1/1970", Now())
-1赞 rothschild86 1/27/2020 #5

为了完整起见,Newtonsoft.Json 也很乐意这样做:

JsonConvert.SerializeObject(DateTime.Now);

(与 XmlConvert 不同,Convert 的两端都使用双引号。

-1赞 Felipe Maricato Moura 9/3/2021 #6
<input asp-for="StartDate" class="form-control" value="@DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss")" />

评论

1赞 Community 9/4/2021
请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
0赞 Andrey Stukalin 11/12/2021 #7

System.Text.Json这样做也是:

JsonSerializer.Serialize(DateTime.Now)
2赞 Pedro Coelho 10/18/2022 #8

这在 .NET 6 中对我有用:

public static class DateTimeExtensions
{
     public static string ToRFC3339(this DateTime date)
     {
         return date.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffK");
     }
}