How to use TimeZoneInfo to get local time during daylight saving time?

I am trying to use DateTimeOffset to transmit a specific point in time in any time zone. I canโ€™t understand how to use TimeZoneInfo to work 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 produces:

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

I am in the central time zone, and now we are in the summer. I am trying to read the second line:

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

BaseUtcOffset does not seem to change based on DST.

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

Thank.

+55
timezone c # datetimeoffset
Jun 02 '10 at 21:45
source share
4 answers

You need to get UtcOffset from TimeZoneInfo, and then pass this 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))); 
+45
Jun 02 2018-10-22T00:
source share

You can also use TimeZoneInfo.ConvertTimeFromUtc, which allows you to use daylight saving time:

 DateTime utc = DateTime.UtcNow; TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, zone); 
+32
May 28 '13 at 10:39
source share

Or better if you don't want to hardcode the time zone identifier:

 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi); 
+5
Mar 11 '15 at 19:53
source share

I am new to both .NET and stackoverflow, so I might be wrong, but here goes:

Using TimeZoneInfo.ConvertTimeFromUtc will allow you to use daylight saving time and convert to the correct time according to the time zone + possible DST offset. However, the offset itself in the resulting object will display the offset for standard time and not include daylight saving. Therefore, if you want to make ToString on the object, you will get the correct time (in hours and minutes), but the wrong offset during daylight saving time, which can lead to the wrong time later in the code.

If you use GetUtcOffset to get the offset within a certain time, and then execute ToOffset on the DateTimeOffset, then both the hours / minutes and the offset itself will be correctly converted, and you can safely execute ToString.

 string ExpectedDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss''zzz"; string timeZoneId = "FLE Standard Time"; string dateTimestr = "2017-10-09T09:00:00+02:00"; DateTimeOffset dto = DateTimeOffset.Parse(dateTimeStr); TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId); TimeSpan offset = zone.GetUtcOffset(dto); dto = dto.ToOffset(offset); string localTime = dto.ToString(ExpectedDateTimePattern); 

localTime will return "2017-10-09T10: 00: 00 + 03: 00".

datetimeoffset timezoneinfo getutcoffset

+1
Nov 07 '17 at 12:03
source share



All Articles