userJoel
userJoel

Reputation: 21

DateTime.Now doesn't respond to time zone changes

I built a clock using DateTime.Now and receive a time zone from a server. The problem is that the DateTime keeps using the old time zone.

(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?

Upvotes: 2

Views: 1678

Answers (3)

Anas Jame
Anas Jame

Reputation: 252

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

Upvotes: 4

Ondrej Tucny
Ondrej Tucny

Reputation: 27974

You need to convert the current local time into the desired timezone. Something like this:

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

Upvotes: 1

vesuvious
vesuvious

Reputation: 2753

Have you tried using the TimeZone class instead of 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 );

Upvotes: 0

Related Questions