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

C#DatetimeoffsetTimezone

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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);

Solution 2 - C#

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)));

Solution 3 - C#

Or better, if you don't want to hard code the time zone identifier:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id);
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);

Solution 4 - C#

I'm a beginner both at .NET and stackoverflow, so I could be wrong, but here goes:

Using 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.

If 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.

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".

[tag:DateTimeOffset] [tag:TimeZoneInfo] [tag:GetUtcOffset]

Solution 5 - C#

This will Adjust automatically... and Return time as per your timezone.

public static string SetLastModified (

TimeZoneInfo csttzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);

DateTime cstTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, csttzi);

return String.Format("DaylightSavingTime: {0}", cstTime.IsDaylightSavingTime().ToString());

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamintoView Question on Stackoverflow
Solution 1 - C#Karl GjertsenView Answer on Stackoverflow
Solution 2 - C#Paul Kearney - pkView Answer on Stackoverflow
Solution 3 - C#PabinatorView Answer on Stackoverflow
Solution 4 - C#Niels PeinView Answer on Stackoverflow
Solution 5 - C#Vinay DixitView Answer on Stackoverflow