Convert UTC Offset to Any Time Zone Offset in C#

time zones

This post contains affiliate links and Generous Dev gets commissions for purchases made through these links in this post. See the Disclaimer page for more information

Time zones offsets are a headache for developers. Client applications are sending data from all different areas of the world, with their own time zone values. You’ll want to capture the user’s time zone. You’ll want to store dates in a UTC time zone offset. You also want to capture your server’s time zone offset too. Using datetime offsets in C# is a great way to handle all of those different potential situations.

The Solution to Converting UTC Time Zone Offsets

https://dotnetfiddle.net/XdZbZW

//Read your UTC DateTimeOffset string *Assuming here that it's formatted correctly
DateTimeOffset utcTime = DateTimeOffset.Parse("10/27/2020 7:51:16 PM +00:00");
Console.WriteLine("Passed in UTC Time is: {0}", utcTime);
//Loop through all time zones to display the utc time converted to each
foreach (var timezone in TimeZoneInfo.GetSystemTimeZones())
{
  DateTimeOffset convertedTime = TimeZoneInfo.ConvertTime(utcTime, timezone);
  Console.WriteLine("{0}: {1}", timezone.DisplayName, convertedTime);
}

What is UTC?

UTC or Coordinated Universal Time is a primary standard of time at 0 degrees longitude. Also know as GMT or Greenwhich Mean Time, UTC is used as a standard way of communicating the current time regardless of location.

What is DateTimeOffset in C#?

A DateTimeOffset object shows the amount of hours before or after the UTC time for a specific time zone. If the hours of the local time zone offset are before the UTC time, they’re representing by a minus (-) symbol. If the hours are after, they’re represented by a plus (+) symbol.

For example, the solution above is converting from UTC (+00:00) to all time zones including EST (-04:00). When the current UTC time is 10:00+00:00, the current EST time will display 4 hours before as 6:00-04:00 during Daylight Savings Time. This is like saying the current time is 10am UTC and 6am EST. The -04:00 offset is just a numeric way of saying EST.

UTC Offset vs. Local Time Zone Offset

In a single column, you can only save your datetime in your database in one format. However, the consumers of your application may be located in many different time zones and want to see this datetime converted to their own offset in the UI. In this scenario you want to store your datetimes in UTC. Then before displaying that time on the UI, convert it to the user’s local time zone offset. If for some reason, you want to capture the time of some transaction in a specific time zone, then you’ll want to store that time zone offset value, rather than UTC.

Time Zone Offsets and Daylight Savings Time

Daylight Savings Time is a practice some time zones enforce by adjusting their offset from UTC forward by one hour. The above solution will factor in daylight savings time automatically, so we’re good there. You can check if a specific datetime is in daylight savings time by using the DateTime.IsDaylightSavingTime method. You can also check if a time zone enforces this practice by using the TimeZoneInfo.IsDaylightSavingTime method.

Recommended Products On This Topic

  

Programmer Humor Apparel

 

Subscribe to be notified of new posts

You can also subscribe the Generous Dev Youtube Channel for free videos.

Leave a Reply

Your email address will not be published.