给定一个 DateTime 对象,如何获取字符串格式的 ISO 8601 日期?
给定:
DateTime.UtcNow
如何获取以 ISO 8601 兼容格式表示相同值的字符串?
请注意,ISO 8601 定义了许多类似的格式。 我正在寻找的具体格式是:
yyyy-MM-ddTHH:mm:ssZ
Given:
DateTime.UtcNow
How do I get a string which represents the same value in an ISO 8601-compliant format?
Note that ISO 8601 defines a number of similar formats. The specific format I am looking for is:
yyyy-MM-ddTHH:mm:ssZ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
应该给你你正在寻找的东西,因为“s”格式说明符被描述为可排序的日期/时间模式; 符合 ISO 8601。
编辑:要按照 OP 的要求在末尾获取附加
Z
,请使用"o"
而不是“s”
。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.
EDIT: To get the additional
Z
at the end as the OP requires, use"o"
instead of"s"
.在这里,您可以以“光荣”的简单方式得到答案
Here you have your answer in its 'glorious' simplicity ????!
正如其他答案中提到的,
DateTime
在设计上存在问题。NodaTime
我建议使用 NodaTime 来管理日期/时间值:
格式
因此,要创建和格式化
ZonedDateTime
,您可以使用以下代码片段:对于我来说,
NodaTime
代码看起来相当冗长。 但类型确实很有用。 它们有助于正确处理日期/时间值。Newtonsoft.Json
As mentioned in other answer,
DateTime
has issues by design.NodaTime
I suggest to use NodaTime to manage date/time values:
Formatting
So, to create and format
ZonedDateTime
you can use the following code snippet:For me
NodaTime
code looks quite verbose. But types are really useful. They help to handle date/time values correctly.Newtonsoft.Json
使用 Newtonsoft.Json,您可以执行
示例: https://dotnetfiddle.net/O2xFSl
Using Newtonsoft.Json, you can do
Example: https://dotnetfiddle.net/O2xFSl
如果您在 SharePoint 2010 或更高版本下进行开发,您可以使用
If you're developing under SharePoint 2010 or higher you can use
要采用类似 2018-06-22T13:04:16 的格式,可以在 API 使用的 URI 中传递:
To format like 2018-06-22T13:04:16 which can be passed in the URI of an API use:
要将 DateTime.UtcNow 转换为 yyyy-MM-ddTHH:mm:ssZ 的字符串表示形式,可以将 DateTime 结构的 ToString() 方法与自定义格式字符串结合使用。 将自定义格式字符串与 DateTime 一起使用时,请务必记住,您需要使用单引号转义分隔符。
以下将返回您想要的字符串表示形式:
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.
The following will return the string represention you wanted:
– 来自 MSDN
– from MSDN
有趣的是,自定义格式“yyyy-MM-ddTHH:mm:ssK”(不含毫秒)是最快的格式方法。
另外有趣的是,“S”格式在 Classic 上很慢,而在 Core 上很快……
当然,数字非常接近,某些行之间的差异微不足道(带有后缀
_Verify
的测试与那些相同)没有该后缀,演示结果的可重复性)代码:
https://github.com/dotnet/BenchmarkDotNet 已使用
It is interesting that custom format "yyyy-MM-ddTHH:mm:ssK" (without ms) is the quickest format method.
Also it is interesting that "S" format is slow on Classic and fast on Core...
Of course numbers are very close, between some rows difference is insignificant (tests with suffix
_Verify
are the same as those that are without that suffix, demonstrates results repeatability)Code:
https://github.com/dotnet/BenchmarkDotNet was used
我只会使用
XmlConvert
:它会自动保留时区。
I would just use
XmlConvert
:It will automatically preserve the time zone.
注意:根据您最后进行的转换,您将使用第一行(最喜欢它)或第二行。
确保仅在当地时间应用格式,因为“zzz”是 UTC 转换的时区信息。
NOTE: Depending on the conversion you are doing on your end, you will be using the first line (most like it) or the second one.
Make sure to applied format only at local time, since "zzz" is the time zone information for UTC conversion.
这些答案中的大多数都有毫秒/微秒,这显然不受 ISO 8601 支持。 正确答案是:
参考文献:
Most of these answers have milliseconds / microseconds which clearly isn't supported by ISO 8601. The correct answer would be:
References:
您可以使用以下命令获取“Z”(ISO 8601 UTC)下一个代码:
原因如下:
ISO 8601 有一些不同的格式:
DateTimeKind.Local
DateTimeKind.Utc
DateTimeKind.Unspecified
.NET 为我们提供了一个包含这些选项的枚举:
注意:如果将 Visual Studio 2008“监视实用程序”应用于 toString("o") 部分你可能会得到不同的结果,我不知道这是否是一个错误,但在这种情况下,如果你正在调试,使用字符串变量会得到更好的结果。
来源:标准日期和时间格式字符串(MSDN)
You can get the "Z" (ISO 8601 UTC) with the next code:
Here is why:
The ISO 8601 have some different formats:
DateTimeKind.Local
DateTimeKind.Utc
DateTimeKind.Unspecified
.NET provides us with an enum with those options:
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.
Source: Standard Date and Time Format Strings (MSDN)
您有几个选项,包括“往返(“O”)格式说明符”。
输出
但是,DateTime + TimeZone 可能会出现博客文章中所述的其他问题.NET 中的 DateTime 和 DateTimeOffset:良好实践和常见陷阱:
You have a few options including the "Round-trip ("O") format specifier".
Output
However, DateTime + TimeZone may present other problems as described in the blog post DateTime and DateTimeOffset in .NET: Good practices and common pitfalls:
令人惊讶的是没有人建议它:
UniversalSortableDateTimePattern 让你几乎一路到达你想要的(这更像是 RFC 3339 表示)。
添加:
我决定使用答案 https://stackoverflow.com/a/43793679/653058 中的基准来比较如何这执行。
TL:博士; 虽然价格昂贵,但在我蹩脚的旧笔记本电脑上仍然只有 650 纳秒多一点:-)
实施:
结果:
Surprised that no one suggested it:
The UniversalSortableDateTimePattern gets you almost all the way to what you want (which is more an RFC 3339 representation).
Added:
I decided to use the benchmarks that were in answer https://stackoverflow.com/a/43793679/653058 to compare how this performs.
tl:dr; it's at the expensive end but still just a little over 650 nanoseconds on my crappy old laptop :-)
Implementation:
Results:
使用 < code>o 标准格式说明符,用于生成符合 ISO 8601 的日期字符串。
如果它必须以
Z
而不是时区偏移(-00:00、+05:00、-07:00
)结尾,则使用 Kind = DateTimeKind 的 DateTime .Utc:要控制小数秒部分的位数,请编辑并使用标准格式说明符的扩展形式,即:
Use the
o
standard format specifier to produce an ISO 8601 compliant date string.If it has to end with
Z
instead of the timezone offset (-00:00, +05:00, -07:00
) then use a DateTime having Kind = DateTimeKind.Utc:To control the number of digits in fractional seconds part, edit and use the expanded form of the standard format specifier which is:
返回类似 2008-04-10T06:30:00
UtcNow
的内容显然返回 UTC< /a> 时间,这样没有坏处:Returns something like 2008-04-10T06:30:00
UtcNow
obviously returns a UTC time so there is no harm in:使用:
输出
来源:
标准日期和时间格式字符串 (MSDN)
自定义日期和时间格式字符串< /a> (MSDN)
Use:
OUTPUT
Sources:
Standard Date and Time Format Strings (MSDN)
Custom Date and Time Format Strings (MSDN)
使用自定义日期时间格式,这会给你一个类似于
的日期
2008-09-22T13:57:31.2311892-04:00。
另一种方法是:
使用标准 “往返”风格(ISO 8601)给你
2008-09-22T14:01:54.9571247Z。
要获取指定的格式,可以使用:
Using custom date-time formatting, this gives you a date similar to
2008-09-22T13:57:31.2311892-04:00.
Another way is:
which uses the standard "round-trip" style (ISO 8601) to give you
2008-09-22T14:01:54.9571247Z.
To get the specified format, you can use: