I have a UTC DateTime value coming from a database record. I also have a user-specified time zone (an instance of TimeZoneInfo). How do I convert that UTC DateTime to the user's local time zone? Also, how do I determine if the user-specified time zone is currently observing DST? I'm using .NET 3.5.
Thanks, Mark
Have a look at the DateTimeOffset structure:
For DST see the TimeZoneInfo.IsDaylightSavingTime method.
The best way to do this is simply to use
TimeZoneInfo.ConvertTimeFromUtc
.The only catch is that the incoming
DateTime
value may not have theDateTimeKind.Local
kind. It must either beUtc
, orUnspecified
.The Antartica answer only works for the timezones matching UTC. I'm quite traumatized with this
DateTimeOffset
function and after hours of trial and error, I've managed to produce a practical conversion extension function that works with all timezones.Example:
You can use a dedicated function within TimeZoneInfo if you want to convert a DateTimeOffset into another DateTimeOffset: