C# 为什么 DateTime.Now.ToString("u") 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/915693/
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-06 03:06:36  来源:igfitidea点击:

Why does DateTime.Now.ToString("u") not work?

c#.netvb.netdatetimeutc

提问by John

I am currently in British summer timewhich is UTC +1 Hour. I confirmed my PC is correct with the following code and it returns true.

我目前处于英国夏令时,即 UTC +1 小时。我用以下代码确认我的电脑是正确的,它返回真。

System.TimeZone.CurrentTimeZone.IsDaylightSavingTime(Date.Now)

My question is then why does the UTC formatter not work as I would expect:

我的问题是为什么 UTC 格式化程序不能像我期望的那样工作:

DateTime.Now.ToString("u")

It returns the exact current system date as below in UTC format as expected but with the Z (Zulu Time) at the end not +01:00?

它按预期以 UTC 格式返回如下确切的当前系统日期,但最后的 Z(祖鲁时间)不是 +01:00?

i.e.

IE

2009-05-27 14:21:22Z

not

不是

2009-05-27 14:21:22+01:00

Is this correct functionality?

这是正确的功能吗?

采纳答案by Patrick McDonald

MSDN states the following:

MSDN 声明如下:

Represents a custom date and time format string defined by the DateTimeFormatInfo.UniversalSortableDateTimePattern property. The pattern reflects a defined standard 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 HH':'mm':'ss'Z'".

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

Formatting does not convert the time zone for the date and time object. Therefore, the application must convert a date and time to Coordinated Universal Time (UTC) before using this format specifier.

表示由 DateTimeFormatInfo.UniversalSortableDateTimePattern 属性定义的自定义日期和时间格式字符串。该模式反映了定义的标准,并且该属性是只读的。因此,无论使用何种区域性或提供的格式提供程序,它总是相同的。自定义格式字符串为“yyyy'-'MM'-'dd HH':'mm':'ss'Z'”。

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

格式化不会转换日期和时间对象的时区。因此,应用程序必须在使用此格式说明符之前将日期和时间转换为协调世界时 (UTC)。

You should use the following code to convert your current Date to UTC before formatting it:

在格式化之前,您应该使用以下代码将当前日期转换为 UTC:

DateTime.UtcNow.ToString("u")

or

或者

DateTime.Now.ToUniversalTime().ToString("u")

To display in the format you expected (i.e. 2009-05-27 14:21:22+01:00), you would need to use a custom date format:

要以您期望的格式显示(即 2009-05-27 14:21:22+01:00),您需要使用自定义日期格式:

DateTime.Now.ToString("yyyy-MM-dd HH:mm:sszzz");

回答by Rowland Shaw

"u" is the Universal sortable date/time pattern, not UTC format; To quote the documentation:

"u" 是通用可排序日期/时间模式,而不是 UTC 格式;引用文档

Represents a custom date and time format string defined by the DateTimeFormatInfo..::.UniversalSortableDateTimePattern property. The pattern reflects a defined standard 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 HH':'mm':'ss'Z'".

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

Formatting does not convert the time zone for the date and time object. Therefore, the application must convert a date and time to Coordinated Universal Time (UTC) before using this format specifier.

表示由 DateTimeFormatInfo..::.UniversalSortableDateTimePattern 属性定义的自定义日期和时间格式字符串。该模式反映了定义的标准,并且该属性是只读的。因此,无论使用何种区域性或提供的格式提供程序,它总是相同的。自定义格式字符串为“yyyy'-'MM'-'dd HH':'mm':'ss'Z'”。

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

格式化不会转换日期和时间对象的时区。因此,应用程序必须在使用此格式说明符之前将日期和时间转换为协调世界时 (UTC)。

回答by Badaro

You need to use DateTime.Now.ToUniversalTime().ToString("u").

您需要使用 DateTime.Now.ToUniversalTime().ToString("u")。