Skip to content

Instantly share code, notes, and snippets.

@smetronic
Last active November 6, 2020 15:30
Show Gist options
  • Save smetronic/3f4c4cb7db69822a12f0a283b75ff23d to your computer and use it in GitHub Desktop.
Save smetronic/3f4c4cb7db69822a12f0a283b75ff23d to your computer and use it in GitHub Desktop.
C# Convert UTC to TimeZone or Vice Versa
public static DateTime ToTimeZoneTime(DateTime time, string timeZoneId = "UTC")
{
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
return TimeZoneInfo.ConvertTimeFromUtc(time, tzi);
}
public static DateTime ToUTC(DateTime time, string timeZoneId = "UTC")
{
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
return TimeZoneInfo.ConvertTimeToUtc(time, tzi);
}
public static Void ListTimeZones()
{
//Reference: https://support.microsoft.com/en-ae/help/973627/microsoft-time-zone-index-values
foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
{
Console.WriteLine(z.Id);
}
}
var startDateTime = DateTime.UtcNow;
var _timeZoneId = "Arabian Standard Time";
var convert = ToTimeZoneTime(startDateTime, _timeZoneId).ToString("yyyy-MM-dd"); // Convert UTC to Time Zone Time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment