为什么以下代码(在 C# 中)返回 false:
DateTime d = DateTime.Now;
d.Ticks == d.ToUniversalTime().Ticks; // false
我希望 DateTime 的刻度基于 UTC 时间。 DateTime.Ticks 上的 MSDN 页面提到说
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.
一月一日的午夜,0001 ..在哪个时区?
为什么 DateTime.Ticks 会依赖于时区?
我猜 Ticks 不同的事实是以下代码也返回 false 的原因
DateTime d = DateTime.Now;
d == d.ToUniversalTime(); // false
DateTime.Equals 上的 MSDN 文档提及
t1 and t2 are equal if their Ticks property values are equal. Their Kind property values are not considered in the test for equality.
我的期望是无论时区如何,DateTime.Ticks 都是相等的。
我希望无论发生在哪个时区,两个时刻都是相等的。我的期望有误吗?
最佳答案
来源:http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/fde7e5b0-e2b9-4d3b-8a63-c2ae75e316d8
DateTime.Ticks is documented as "number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001". That is 1-Jan-0001 local time. If you convert your DateTime to UTC, Ticks will then be number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 UTC. Potentially different that 1-Jan-0001 local time, ergo the two Ticks values will be different.
关于c# - DateTime.Ticks、DateTime.Equals 和时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10251851/