C# 给定一个 DateTime 对象,如何以字符串格式获取 ISO 8601 日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/114983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:24:10  来源:igfitidea点击:

Given a DateTime object, how do I get an ISO 8601 date in string format?

提问by Iain

Given:

鉴于:

DateTime.UtcNow

How do I get a string which represents the same value in an ISO 8601-compliant format?

如何获取以ISO 8601兼容格式表示相同值的字符串?

Note that ISO 8601 defines a number of similar formats. The specific format I am looking for is:

请注意,ISO 8601 定义了许多类似的格式。我正在寻找的具体格式是:

yyyy-MM-ddTHH:mm:ssZ

回答by Iain

DateTime.UtcNow.ToString("s")

Returns something like 2008-04-10T06:30:00

返回类似 2008-04-10T06:30:00 的内容

UtcNowobviously returns a UTCtime so there is no harm in:

UtcNow显然返回一个UTC时间所以没有坏处:

string.Concat(DateTime.UtcNow.ToString("s"), "Z")

回答by Wayne

Note to readers:Several commenters have pointed out some problems in this answer (related particularly to the first suggestion). Refer to the comments section for more information.

读者注意:一些评论者指出了这个答案中的一些问题(特别是与第一个建议有关)。有关更多信息,请参阅评论部分。

DateTime.UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffzzz");

This gives you a date similar to 2008-09-22T13:57:31.2311892-04:00.

这为您提供了类似于2008-09-22T13:57:31.2311892-04:00 的日期

Another way is:

另一种方式是:

DateTime.UtcNow.ToString("o");

which gives you 2008-09-22T14:01:54.9571247Z

这给你2008-09-22T14:01:54.9571247Z

To get the specified format, you can use:

要获取指定的格式,您可以使用:

DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")

DateTime Formatting Options

日期时间格式选项

回答by Simon Wilson

DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture)should give you what you are looking for as the "s" format specifier is described as a sortable date/time pattern; conforms to ISO 8601.

DateTime.UtcNow.ToString("s", System.Globalization.CultureInfo.InvariantCulture)应该给你你正在寻找的东西,因为“s”格式说明符被描述为一个可排序的日期/时间模式;符合 ISO 8601。

回答by Oppositional

To convert DateTime.UtcNow to a string representation of yyyy-MM-ddTHH:mm:ssZ, you can use the ToString() method of the DateTime structure with a custom formatting string. When using custom format strings with a DateTime, it is important to remember that you need to escape your seperators using single quotes.

要将 DateTime.UtcNow 转换为yyyy-MM-ddTHH:mm:ssZ的字符串表示形式,您可以使用带有自定义格式字符串的 DateTime 结构的 ToString() 方法。将自定义格式字符串与 DateTime 一起使用时,请务必记住您需要使用单引号对分隔符进行转义。

The following will return the string represention you wanted:

以下将返回您想要的字符串表示:

DateTime.UtcNow.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", DateTimeFormatInfo.InvariantInfo)

回答by Sumrak

I would just use XmlConvert:

我只会使用XmlConvert

XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.RoundtripKind);

It will automatically preserve the time zone.

它会自动保留时区。

回答by Amal

The "s"standard format specifier represents a custom date and time format string that is defined by the DateTimeFormatInfo.SortableDateTimePatternproperty. The pattern reflects a defined standard (ISO 8601), and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".

When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.

"s"标准格式说明代表在由定义的自定义的日期和时间的格式字符串DateTimeFormatInfo.SortableDateTimePattern属性。该模式反映了定义的标准 ( ISO 8601),并且该属性是只读的。因此,无论使用何种区域性或提供的格式提供程序,它总是相同的。自定义格式字符串是"yyyy'-'MM'-'dd'T'HH':'mm':'ss".

使用此标准格式说明符时,格式化或解析操作始终使用不变区域性。

– from MSDN

– 来自MSDN

回答by Don

Use:

用:

private void TimeFormats()
{
    DateTime localTime = DateTime.Now;
    DateTime utcTime = DateTime.UtcNow;
    DateTimeOffset localTimeAndOffset = new DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset(localTime));

    //UTC
    string strUtcTime_o = utcTime.ToString("o");
    string strUtcTime_s = utcTime.ToString("s");
    string strUtcTime_custom = utcTime.ToString("yyyy-MM-ddTHH:mm:ssK");

    //Local
    string strLocalTimeAndOffset_o = localTimeAndOffset.ToString("o");
    string strLocalTimeAndOffset_s = localTimeAndOffset.ToString("s");
    string strLocalTimeAndOffset_custom = utcTime.ToString("yyyy-MM-ddTHH:mm:ssK");

    //Output
    Response.Write("<br/>UTC<br/>");
    Response.Write("strUtcTime_o: " + strUtcTime_o + "<br/>");
    Response.Write("strUtcTime_s: " + strUtcTime_s + "<br/>");
    Response.Write("strUtcTime_custom: " + strUtcTime_custom + "<br/>");

    Response.Write("<br/>Local Time<br/>");
    Response.Write("strLocalTimeAndOffset_o: " + strLocalTimeAndOffset_o + "<br/>");
    Response.Write("strLocalTimeAndOffset_s: " + strLocalTimeAndOffset_s + "<br/>");
    Response.Write("strLocalTimeAndOffset_custom: " + strLocalTimeAndOffset_custom + "<br/>");

}

OUTPUT

输出

UTC
    strUtcTime_o: 2012-09-17T22:02:51.4021600Z
    strUtcTime_s: 2012-09-17T22:02:51
    strUtcTime_custom: 2012-09-17T22:02:51Z

Local Time
    strLocalTimeAndOffset_o: 2012-09-17T15:02:51.4021600-07:00
    strLocalTimeAndOffset_s: 2012-09-17T15:02:51
    strLocalTimeAndOffset_custom: 2012-09-17T22:02:51Z

Sources:

资料来源:

回答by Simon Logic

If you're developing under SharePoint 2010or higher you can use

如果您在SharePoint 2010或更高版本下进行开发,则可以使用

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
...
string strISODate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now)

回答by Henrik

System.DateTime.UtcNow.ToString("o")

=>

=>

val it : string = "2013-10-13T13:03:50.2950037Z"

回答by Oaxas

You can get the "Z" (ISO 8601UTC) with the next code:

您可以使用下一个代码获得“Z”(ISO 8601 UTC):

Dim tmpDate As DateTime = New DateTime(Now.Ticks, DateTimeKind.Utc)
Dim res as String = tmpDate.toString("o") '2009-06-15T13:45:30.0000000Z



Here is why:

原因如下:

The ISO 8601 have some different formats:

ISO 8601 有一些不同的格式:

DateTimeKind.Local

日期时间种类.本地

2009-06-15T13:45:30.0000000-07:00

DateTimeKind.Utc

日期时间种类.Utc

2009-06-15T13:45:30.0000000Z

DateTimeKind.Unspecified

DateTimeKind.Unspecified

2009-06-15T13:45:30.0000000



.NET provides us with an enum with those options:

.NET 为我们提供了一个带有这些选项的枚举:

'2009-06-15T13:45:30.0000000-07:00
Dim strTmp1 As String = New DateTime(Now.Ticks, DateTimeKind.Local).ToString("o")

'2009-06-15T13:45:30.0000000Z
Dim strTmp2 As String = New DateTime(Now.Ticks, DateTimeKind.Utc).ToString("o")

'2009-06-15T13:45:30.0000000
Dim strTmp3 As String = New DateTime(Now.Ticks, DateTimeKind.Unspecified).ToString("o")

Note: If you apply the Visual Studio 2008 "watch utility" to the toString("o")part you may get different results, I don't know if it's a bug, but in this case you have better results using a String variable if you're debugging.

注意:如果您将 Visual Studio 2008“监视实用程序”应用于toString("o")部分,您可能会得到不同的结果,我不知道这是否是一个错误,但在这种情况下,使用 String 变量可以获得更好的结果如果你正在调试。

Source: Standard Date and Time Format Strings(MSDN)

来源:标准日期和时间格式字符串(MSDN)