DateTime - ConvertTimeFromUtc
Converts a Coordinated Universal Time (UTC) to the time in a specified time zone.
public static void Main() { DateTime timeUtc = DateTime.UtcNow; try { TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); // C# Extension Method: DateTime - ConvertTimeFromUtc DateTime cstTime = timeUtc.ConvertTimeFromUtc(cstZone); Console.WriteLine("The date and time are {0} {1}.", cstTime, 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."); } }
View Source
using System; public static partial class Extensions { /// <summary> /// Converts a Coordinated Universal Time (UTC) to the time in a specified time zone. /// </summary> /// <param name="dateTime">The Coordinated Universal Time (UTC).</param> /// <param name="destinationTimeZone">The time zone to convert to.</param> /// <returns> /// The date and time in the destination time zone. Its property is if is ; otherwise, its property is . /// </returns> public static DateTime ConvertTimeFromUtc(this DateTime dateTime, TimeZoneInfo destinationTimeZone) { return TimeZoneInfo.ConvertTimeFromUtc(dateTime, destinationTimeZone); } }