Last active
November 6, 2020 15:30
-
-
Save smetronic/3f4c4cb7db69822a12f0a283b75ff23d to your computer and use it in GitHub Desktop.
C# Convert UTC to TimeZone or Vice Versa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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