Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TimeZoneInfo to get local time during Daylight Saving Time?

I'm trying to use DateTimeOffset to convey a specific moment in time across any time zone. I can't figure out how to use TimeZoneInfo to deal with daylight saving time.

var dt = DateTime.UtcNow; Console.WriteLine(dt.ToLocalTime());  var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero); Console.WriteLine(utcOffset.ToOffset(tz.BaseUtcOffset)); 

This prints out:

6/2/2010 4:37:19 PM 6/2/2010 3:37:19 PM -06:00

I am in the central time zone, and and we are currently in daylight saving time. I am trying to get the second line to read:

6/2/2010 4:37:19 PM -05:00

BaseUtcOffset apparently doesn't change based on DST.

How can I get the the right time with the proper offset value?

like image 261
jaminto Avatar asked Jun 02 '10 21:06

jaminto


People also ask

Does timezone offset change with daylight savings?

However, you need to know what happens during daylight saving time in the United States. In short, the local time is advanced one hour during daylight saving time. As an example, the Eastern Time zone difference from UTC is -4 hours during daylight saving time rather than -5 hours as it is during standard time.

How do you set the clock for daylight savings time?

In the United States, switching between standard and daylight saving time occurs on specific weekends: Spring Forward: Clocks are set ahead by one hour at 2 a.m. on the second Sunday in March. Fall Back: Clocks are set back by one hour at 2 a.m. on the first Sunday in November.

How does UTC time work with daylight savings?

UTC has no DST because it serves as a constant frame of reference in which other time zones are relative. Take note that Coordinated Universal Time does not change with a change of seasons.

How do you fix daylight savings time not observed by this time zone?

In Control Panel, select Clock, Language, and Region. Select Change the time zone. In the Time Zone area of the Date and Time dialog box, select Change time zone. In the Time zone list, select a time zone that does not observe daylight saving time and has the same UTC offset as your current time zone.


2 Answers

You need to get the UtcOffset from the TimeZoneInfo, then pass that to the ToOffset() method:

var dt = DateTime.UtcNow; Console.WriteLine(dt.ToLocalTime());  var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero); Console.WriteLine(utcOffset.ToOffset(tz.GetUtcOffset(utcOffset))); 
like image 97
Paul Kearney - pk Avatar answered Sep 20 '22 11:09

Paul Kearney - pk


You can also use TimeZoneInfo.ConvertTimeFromUtc, which will allow for daylight saving time:

DateTime utc = DateTime.UtcNow; TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, zone); 
like image 20
Karl Gjertsen Avatar answered Sep 22 '22 11:09

Karl Gjertsen