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
Given a DateTime object, how do I get an ISO 8601 date in string format?
提问by Iain
回答by Iain
回答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")
回答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:
资料来源:
标准日期和时间格式字符串(MSDN)
自定义日期和时间格式字符串(MSDN)
回答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)