c# - C# 中的这种日期行为有什么问题?

标签 c# .net-3.5

如果我输出格式化日期如下:

DateTime.Parse("2010-06-02T15:26:37.789 +01:00").ToString("HH:mm:sszzz")

我得到了预期的结果:

15:26:37+01:00

但是,如果我解析相同的日期,则转换为 UTC 并以相同的格式输出,如下所示:

DateTime.Parse("2010-06-02T15:26:37.789 +01:00").ToUniversalTime().ToString("HH:mm:sszzz")

我明白了:

14:26:37+01:00

现在这两个日期(本地版本和 UTC 版本)应该完全相同,但输出的文本代表两个不同的时间。

这是为什么?

编辑

为了澄清,我预计 UTC 时间为 14:26:37,因为 DST 元素已被 UTC 删除。没想到还有偏移。上述两个时间并不相等,而 15:26:37+01:00 和 14:26:37+00:00 em> 是。

最佳答案

好的,现在作为答案:MSDN像这样解释“zzz”:

With DateTime values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. It does not reflect the value of an instance's DateTime.Kind property. For this reason, the "zzz" format specifier is not recommended for use with DateTime values.

Empasi是我的。 英语不是我的母语,但我将其理解为“zzz”是机器的偏移量,与日期时间值根本无关。所以 - 是的,它会是一样的..

关于c# - C# 中的这种日期行为有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2646202/

相关文章:

c# - 使用 MVC 4 在 C# 中使用 Session 进行 foreach 循环

c# - 将多个 EF 上下文注册到一个 DI 容器中

c# - ASP.NET 产品网站的路由规则

c# - 不允许使用默认参数说明符

WCF 服务内存泄漏

c# - 查找引用最多的方法

C# 事件和类方法

c# - 在哪里存储应用程序设置

c# - 使用 LINQ 连接 List<string> 中的所有字符串

c# - 从 asp.net 中的 url 中删除查询字符串