Created
February 7, 2019 07:10
-
-
Save mishrsud/d2189edc39ec73e4094566597070fc66 to your computer and use it in GitHub Desktop.
ToUniversalTime() Isn't Reliable, always use TimzoneInfo.ConvertXXX methods
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 class Program | |
{ | |
public static void Main() | |
{ | |
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(new DateTime(2019,2,2, 0,0,0), TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time"))); | |
Console.WriteLine(TimeZoneInfo.ConvertTimeToUtc(new DateTime(2019,2,2, 23,59,59), TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time"))); | |
Console.WriteLine(new DateTime( | |
2019, | |
2, | |
2, | |
0, | |
0, | |
0).ToUniversalTime()); | |
Console.WriteLine(new DateTime( | |
2019, | |
2, | |
2, | |
23, | |
59, | |
59).ToUniversalTime()); | |
} | |
public static void EchoSystemTimezones() | |
{ | |
foreach (var info in TimeZoneInfo.GetSystemTimeZones()) | |
{ | |
Console.WriteLine(info + "|" + info.Id); | |
} | |
} | |
public static DateTimeOffset ConvertToUtcDateTimeOffset(DateTimeOffset? date) | |
{ | |
return date.HasValue ? TimeZoneInfo.ConvertTimeToUtc(date.Value.DateTime) : DateTimeOffset.UtcNow; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment