c# - DateTime.Ticks、DateTime.Equals 和时区

标签 c# datetime date

为什么以下代码(在 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/

相关文章:

java - 用于保持时间戳完整性的校验和

Bash,如何使用不同的语言环境打印日期

c# - 无法从 GetCustomAttributes 取回属性

C# 以编程方式取消 Windows 关机

python - 使用 Python 编辑 Parquet 文件会导致日期时间格式错误

wpf - 将 WPF Toolkit 日期选择器的值重置为 'default' 值

javascript - 时刻js返回总是零时间

javascript - Typeof 错误 - 返回 NaN,即使两者都是数字

c# - 在 LINQ 中编写 SQL 查询

c# - 如何从程序集中只公开一个特定的类?