Skip to content

Instantly share code, notes, and snippets.

@mishrsud
Created February 7, 2019 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishrsud/d2189edc39ec73e4094566597070fc66 to your computer and use it in GitHub Desktop.
Save mishrsud/d2189edc39ec73e4094566597070fc66 to your computer and use it in GitHub Desktop.
ToUniversalTime() Isn't Reliable, always use TimzoneInfo.ConvertXXX methods
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