简体   繁体   English

DateTime.now不响应时区更改

[英]DateTime.now doesnt respont to time zone changes

I build a clock using DateTime.now and receive a time zone from a server. 我使用DateTime.now构建一个时钟并从服务器接收时区。 the problem is that the DateTime still keeps using the old time zone. 问题是DateTime仍然使用旧时区。

(I want it to start using the new time zone set when I receive without having to reboot) (我希望它在我收到时开始使用新的时区设置,而不必重新启动)

How do I get around this problem? 我该如何解决这个问题?

You have to Clear the Time Cash after changing Time. 您必须在更改时间后清除时间现金。 Use the following Command: 使用以下命令:

TimeZoneInfo.ClearCachedData()

for more details: http://www.c-sharpcorner.com/Forums/Thread/240283/datetime-is-not-reflected-instantly-when-time-zone-changes.aspx 有关详细信息,请访问: http//www.c-sharpcorner.com/Forums/Thread/240283/datetime-is-not-reflected-instantly-when-time-zone-changes.aspx

You need to convert the current local time into the desired timezone. 您需要当前本地时间转换为所需的时区。 Something like this: 像这样的东西:

var timeInServerTimezone = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfoReceivedFromServer);

Have you tried using the TimeZone class instead of DateTime? 您是否尝试过使用TimeZone类而不是DateTime? I am not sure if it will make a difference but worth taking a look. 我不确定它是否会有所作为,但值得一看。

 DateTime baseUTC = new DateTime( 2000, 1, 1 );
 TimeZone localZone = TimeZone.CurrentTimeZone;
 DateTime localTime = localZone.ToLocalTime( baseUTC );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM