c# - Convert UTC DateTime to another Time Zone

ID : 131367

viewed : 10

Tags : c#datetimetimezonedstc#

Top 5 Answer for c# - Convert UTC DateTime to another Time Zone

vote vote


The best way to do this is simply to use TimeZoneInfo.ConvertTimeFromUtc.

// you said you had these already DateTime utc = new DateTime(2014, 6, 4, 12, 34, 0); TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");  // it's a simple one-liner DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi); 

The only catch is that the incoming DateTime value may not have the DateTimeKind.Local kind. It must either be Utc, or Unspecified.

vote vote


You can use a dedicated function within TimeZoneInfo if you want to convert a DateTimeOffset into another DateTimeOffset:

DateTimeOffset newTime = TimeZoneInfo.ConvertTime(     DateTimeOffset.UtcNow,      TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time") ); 
vote vote


Have a look at the DateTimeOffset structure:

// user-specified time zone TimeZoneInfo southPole =     TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");  // an UTC DateTime DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);  // DateTime with offset DateTimeOffset dateAndOffset =     new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));  Console.WriteLine(dateAndOffset); 

For DST see the TimeZoneInfo.IsDaylightSavingTime method.

bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow); 
vote vote


The Antartica answer only works for the timezones matching UTC. I'm quite traumatized with this DateTimeOffset function and after hours of trial and error, I've managed to produce a practical conversion extension function that works with all timezones.

static public class DateTimeFunctions {     static public DateTimeOffset ConvertUtcTimeToTimeZone(this DateTime dateTime, string toTimeZoneDesc)     {         if (dateTime.Kind != DateTimeKind.Utc) throw new Exception("dateTime needs to have Kind property set to Utc");         var toUtcOffset = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc).GetUtcOffset(dateTime);         var convertedTime = DateTime.SpecifyKind(dateTime.Add(toUtcOffset), DateTimeKind.Unspecified);         return new DateTimeOffset(convertedTime, toUtcOffset);     } } 


var currentTimeInPacificTime = DateTime.UtcNow.ConvertUtcTimeToTimeZone("Pacific Standard Time"); 
vote vote


Here's another gotcha: If you're running your code on a Linux server, you need to use the Linux system for timezone names. For example, "Central Standard Time" would be "America/Chicago". The tz list is here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Here's an example with the isWindows switch:

public static class DateTimeHelper {     public static string ConvertUtcToCst(this string dateTimeString)     {         if (string.IsNullOrWhiteSpace(dateTimeString))         {             return string.Empty;         }          if (DateTime.TryParse(dateTimeString, out DateTime outputDateTime))         {             try             {                 var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);                 TimeZoneInfo cstZone = null;                 if (isWindows)                 {                     cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");                 }                 else                 {                     cstZone = TimeZoneInfo.FindSystemTimeZoneById("America/Chicago");                 }                  var cstDateTime = TimeZoneInfo.ConvertTimeFromUtc(outputDateTime, cstZone);                 return cstDateTime.ToString();             }             catch (TimeZoneNotFoundException)             {                 return "The registry does not define the Central Standard Time zone.";             }             catch (InvalidTimeZoneException)             {                 return "Registry data on the Central Standard Time zone has been corrupted.";             }             catch (Exception ex)             {                 return $"Error :: {ex.Message} :: {ex.ToString()}";             }         }          return string.Empty;     } } 

Top 3 video Explaining c# - Convert UTC DateTime to another Time Zone