Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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