.NET 2.0 日期时间 UTC 转换
为什么ToUniversalTime函数在这里没有作用;
DateTime dt = new DateTime(2009,3,24,1,0,0,DateTimeKind.Local);
dt = dt.ToUniversalTime(); // convert BST to UTC ?
dt.ToString();
“24/03/2009 01:00:00”...错了?
与...“24/03/2009 01:00:00”相同
DateTime dt = new DateTime(2009,3,24,1,0,0,DateTimeKind.Utc);
dt = dt.ToUniversalTime(); // nothing to do, already utc
dt.ToString();
...正确。
我预计会对第一个示例的 ToString() 值进行调整,其中指定为 Local 的 DateTime 将在调用 ToUniversalTime() 时产生相应的 TimeZone 计算,并且英国的时间应该导致 “24/03/2009 00:00:00”作为 UTC。
然而,以这种方式指定 DateTimeKind 似乎会使 ToUniversalTime 或 ToLocalTime 无法进行任何计算。
Why does the ToUniversalTime function have no effect here;
DateTime dt = new DateTime(2009,3,24,1,0,0,DateTimeKind.Local);
dt = dt.ToUniversalTime(); // convert BST to UTC ?
dt.ToString();
"24/03/2009 01:00:00" ... wrong?
Is the same as..
DateTime dt = new DateTime(2009,3,24,1,0,0,DateTimeKind.Utc);
dt = dt.ToUniversalTime(); // nothing to do, already utc
dt.ToString();
"24/03/2009 01:00:00" ... correct.
I expected there to be an adjustment to the ToString() value of the first example, where by the DateTime specified as Local would result in a corresponding TimeZone calculation upon the call to ToUniversalTime() and the time in the UK should have resulted in
"24/03/2009 00:00:00" as UTC.
However it appears like the specifying of the DateTimeKind in this way renders ToUniversalTime or ToLocalTime unable to make any calculation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有机会在英国吗? 尽管我们现在处于夏令时,但您在代码中指定的日期是在此切换之前,因此英国的本地时间和 UTC 时间是相同的。 如果您指定四月为月份,那么您将看到一小时的差异。
Are you in the UK by any chance? Although we are now in daylight saving time, the date you specify in your code is before this switched over, so local and UTC time in the UK are the same. If you specify April as your month, then you will see a one hour difference.
干杯大卫·M。
没吃早餐。 事实上,当我使用经过 BST 夏季阈值的日期重复测试时,行为当然是正确的。
为了确认这一点,
ToString()
方法似乎根据 Kind 属性进行输出。Cheers David M.
Not had my breakfast. Indeed, when I repeat the test with dates that elapse the BST summer-time threshold, the behaviour is of course correct.
And to confirm, the
ToString()
method appears to output based on the Kind property.