gpt4 book ai didi

c# - 冬令时/夏令时的 DateTime.ToLocalTime()

转载 作者:太空狗 更新时间:2023-10-29 18:32:43 26 4
gpt4 key购买 nike

我正在使用 DateTime.ToLocalTime() 将日期从 UTC 时间转换为本地时间。我的时区是 GMT+1(贝尔格莱德、布达佩斯、卢比纳...),它在 Windows 设置 (XP) 中设置正确。

在我们所在时区的上周末,我们将冬令时改为夏令时,这意味着我们将本地时间拨慢一小时。

正如我所见,ToLocalTime 方法从那一刻开始表现得很奇怪。当我用它来转换这个冬令时之后的日期时,效果很好,如下所示:

var utcDate2 = new DateTime(2011, 11, 2, 9, 0, 0,DateTimeKind.Utc);

utcDate1.ToLocalTime() 值为:2011.11.02。 10:00:00 正确

Burt 当我想在此更改之前转换日期(例如夏令时的日期)时,它会返回一个错误的值,如下所示:

var utcDate1 = new DateTime(2011, 10, 23, 9, 0, 0,DateTimeKind.Utc);

utcDate2.ToLocalTime() 值为:2011.10.23。 10:00:00 不正确应该是 2011.10.23。 11:00:00

我应该怎么做才能获得正确的值?我应该如何使用 ToLocalTime 来调整冬令时/夏令时?

最佳答案

http://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx

On Windows XP systems, the ToLocalTime method recognizes only the current adjustment rule when converting from UTC to local time. As a result, conversions for periods before the current adjustment rule came into effect may not accurately reflect the difference between UTC and local time.

所以你必须找到另一种方法来解决这个问题。

关于c# - 冬令时/夏令时的 DateTime.ToLocalTime(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com