Home:ALL Converter>How to use TimeZoneInfo to get local time during Daylight Saving Time?

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

Ask Time:2010-06-03T05:45:43         Author:jaminto

Json Formatter

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?

Author:jaminto,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2961848/how-to-use-timezoneinfo-to-get-local-time-during-daylight-saving-time
Karl Gjertsen :

You can also use TimeZoneInfo.ConvertTimeFromUtc, which will allow for daylight saving time:\n\nDateTime utc = DateTime.UtcNow;\nTimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(\"Central Standard Time\");\nDateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, zone);\n",
2013-05-28T10:39:45
Paul Kearney - pk :

You need to get the UtcOffset from the TimeZoneInfo, then pass that to the ToOffset() method:\n\nvar dt = DateTime.UtcNow;\nConsole.WriteLine(dt.ToLocalTime());\n\nvar tz = TimeZoneInfo.FindSystemTimeZoneById(\"Central Standard Time\");\nvar utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);\nConsole.WriteLine(utcOffset.ToOffset(tz.GetUtcOffset(utcOffset)));\n",
2010-06-02T22:15:33
Pabinator :

Or better, if you don't want to hard code the time zone identifier:\n\nTimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id);\nDateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);\n",
2015-03-11T19:53:18
Niels Pein :

I'm a beginner both at .NET and stackoverflow, so I could be wrong, but here goes:\n\nUsing TimeZoneInfo.ConvertTimeFromUtc will allow for daylight saving time, and convert to the correct time according to the time zone + a possible DST offset. However - the offset itself in the resulting object will show the offset for standard time, and not take daylight saving time into account. So if you want to do a ToString on the object, you will end up with the correct time (in hours and minutes), but the wrong offset during daylight saving time, which may lead to the wrong moment in time later in the code.\n\nIf you instead use the GetUtcOffset to get the offset for a specific time, and then do a ToOffset on the DateTimeOffset object, both the hours/minutes and the offset itself will be correctly converted, and you can safely do a ToString.\n\nstring ExpectedDateTimePattern = \"yyyy'-'MM'-'dd'T'HH':'mm':'ss''zzz\";\nstring timeZoneId = \"FLE Standard Time\";\nstring dateTimestr = \"2017-10-09T09:00:00+02:00\";\n\nDateTimeOffset dto = DateTimeOffset.Parse(dateTimeStr);\nTimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);\nTimeSpan offset = zone.GetUtcOffset(dto);\ndto = dto.ToOffset(offset);\nstring localTime = dto.ToString(ExpectedDateTimePattern);\n\n\nlocalTime will return \"2017-10-09T10:00:00+03:00\".\n\ndatetimeoffset timezoneinfo getutcoffset",
2017-11-07T12:03:54
yy