Mega Code Archive

 
Categories / C# / Date Time
 

Converts a Coordinated Universal Time (UTC) to the time in a specified time zone

using System; public class Example {    public static void Main()    {         DateTime timeUtc = DateTime.UtcNow;         try         {            TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");            DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);            Console.WriteLine("The date and time are {0} {1}.",                               cstTime,                               cstZone.IsDaylightSavingTime(cstTime) ? cstZone.DaylightName : cstZone.StandardName);         }         catch (TimeZoneNotFoundException)         {            Console.WriteLine("The registry does not define the Central Standard Time zone.");         }                                    catch (InvalidTimeZoneException)         {            Console.WriteLine("Registry data on the Central STandard Time zone has been corrupted.");         }    } }