.net 如何解析日期时间并将其转换为RFC 3339日期时间格式?
如何将DateTime结构转换为其等效的格式化字符串表示形式和/或将此字符串表示形式解析回结构?RFC-3339日期-时间格式用于许多规范中,例如。这是如何在C#中解析日期时间并将其转换为RFC-3339表示形式的实现。它唯一的限制是日期时间采用协调世界时(UTC).net 如何解析日期时间并将其转换为RFC 3339日期时间格式?,.net,datetime,rfc3339,atom-feed,.net,Datetime,Rfc3339,Atom Feed,如何将DateTime结构转换为其等效的格式化字符串表示形式和/或将此字符串表示形式解析回结构?RFC-3339日期-时间格式用于许多规范中,例如。这是如何在C#中解析日期时间并将其转换为RFC-3339表示形式的实现。它唯一的限制是日期时间采用协调世界时(UTC) 使用系统; 利用制度全球化; 命名空间DateTimeConsoleApplication { /// ///提供用于在等效RFC 3339字符串表示形式之间转换结构的方法。 /// 公共静态类Rfc3339DateTime {
使用系统;
利用制度全球化;
命名空间DateTimeConsoleApplication
{
///
///提供用于在等效RFC 3339字符串表示形式之间转换结构的方法。
///
公共静态类Rfc3339DateTime
{
//============================================================
//私人成员
//============================================================
#区域私人成员
///
///私有成员,用于保存RFC 3339日期时间表示形式符合的格式数组。
///
私有静态字符串[]格式=新字符串[0];
///
///私有成员,用于保存DateTime格式字符串,以RFC 3339格式表示日期时间。
///
private const string format=“yyyy-MM-dd'T'HH:MM:ss.fffK”;
#端区
//============================================================
//公共财产
//============================================================
#区域Rfc3339DateTimeFormat
///
///获取自定义格式说明符,该说明符可用于表示RFC 3339格式的。
///
///日期时间格式字符串,可用于表示RFC 3339格式的。
///
///
///此方法返回一个
///精确到秒分数的三个最高有效位;也就是说,它表示
///日期和时间值中的毫秒。是有效的
///方法中使用的日期时间格式字符串。
///
///
公共静态字符串Rfc3339DateTimeFormat
{
得到
{
返回格式;
}
}
#端区
#区域Rfc3339DateTimePatterns
///
///获取RFC 3339日期时间字符串表示形式的预期格式数组。
///
///
///RFC 3339日期时间字符串表示的预期格式数组
///在方法中可能使用的。
///
公共静态字符串[]Rfc3339DateTimePatterns
{
得到
{
如果(formats.Length>0)
{
返回格式;
}
其他的
{
格式=新字符串[11];
//Rfc3339DateTimePatterns
格式[0]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'fffffff k”;
格式[1]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'ffffff k”;
格式[2]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.”“fffff k”;
格式[3]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'ffffK”;
格式[4]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'fffK”;
格式[5]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'ffK”;
格式[6]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'fK”;
格式[7]=“yyyy'-'MM'-'dd'T'HH':'MM':'ssK”;
//后退模式
格式[8]=“yyyy'-'MM'-'dd'T'HH':'MM':'ss.'fffffff k”;//RoundtripDateTimePattern
格式[9]=DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern;
格式[10]=DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern;
返回格式;
}
}
}
#端区
//============================================================
//公共方法
//============================================================
#区域解析(字符串s)
///
///将日期和时间的指定字符串表示形式转换为其等效形式。
///
///包含要转换的日期和时间的字符串。
///A相当于中包含的日期和时间。
///
///使用对象中的格式信息解析字符串。
///
///是空引用(在Visual Basic中为空)。
///不包含日期和时间的有效RFC 3339字符串表示形式。
公共静态日期时间分析(字符串s)
{
//------------------------------------------------------------
//验证参数
//------------------------------------------------------------
如果(s==null)
{
抛出新的ArgumentNullException(“s”);
}
日期时间结果;
if(Rfc3339DateTime.TryParse(s,输出结果))
{
返回结果;
}
其他的
{
抛出新的FormatException(String.Format(null,{0}不是日期和时间的有效RFC 3339字符串表示形式。“,s));
}
}
#端区
#区域到字符串(日期时间utcDateTime)
///
///将指定对象的值转换为其等效的字符串表示形式。
///
///要转换的协调世界时(UTC)。
///的值的RFC 3339字符串表示形式。
///
///
///此方法返回该对象的字符串表示形式
///精确到秒分数的三个最高有效位;也就是说,它表示
///日期和时间中的毫秒数
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
}
}
datetime.ToString("YYYY-MM-DD'T'HH:mm:ssZ")
XmlConvert.ToDateTime(string s, XmlDateTimeSerializationMode dateTimeOption)
XmlConvert.ToString(DateTime value, XmlDateTimeSerializationMode dateTimeOption)
rfcFormat = DateDiff("s", "1/1/1970", Now())
JsonConvert.SerializeObject(DateTime.Now);