.NET DateTime.Now returns incorrect time when time zone is changed
This problem occurred during daylight saving time change. After the change occurred, we've noticed that our server application started writing into the log incorrect time - one hour ahead which means that .NET caches time zone offset. We had to restart our application to resolve this problem. I wrote a simple application to reproduce this problem. When I change the time zone while application is running, DateTime.Now property keeps producing the time in the old time zone. Does anybody know if there is a workaround for this problem apart from restarting the application?
Solution 1:
Yes, the current time zone is cached. For a good reason, it avoids trouble with broken code that uses DateTime.Now to implement elapsed time measurement. Such code tends to suffer a heart-attack when the time suddenly changes by an hour or more.
You will have to call System.Globalization.CultureInfo.ClearCachedData() to reset the cached value. The next call to DateTime.Now will now give the new local time. If you use the .NET 3.5 TimeZoneInfo class at all then you'll also need to call its ClearCachedData() method. You can use the SystemEvents.TimeChanged event as a trigger.
Solution 2:
The most common recommendation is to store DateTime.UtcNow and, when you want to show localized time to the user, convert to local time accounting for daylight savings.
.NET provides for calculations involving daylight savings time with the DaylightTime and TimeZone classes, and the ToLocalTime method supposedly can convert UTC to local accounting for daylight savings time.
Solution 3:
The fully qualified class above was slightly off, but perhaps it changed in .NET 3.5.
System.Globalization.CultureInfo.CurrentCulture.ClearCachedData()
also don't forget to include (in C#.NET) or import (with VB.NET) the library reference System.Globalization.CultureInfo
Call it just before using DateTime.Now
. Although it's probably best to call it in the startup event of your Global.asax file.
========== Also make sure you're checking the timezone on Windows Server itself, or your local machine depending on where the IIS web server is running.