Skip to content

Instantly share code, notes, and snippets.

@binki
Created November 5, 2019 15:12
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 binki/d0440e4f69b678285d07cb3e7f835352 to your computer and use it in GitHub Desktop.
Save binki/d0440e4f69b678285d07cb3e7f835352 to your computer and use it in GitHub Desktop.
Fun with TimeZoneInfo
in=11/1/2019 10:00:00 AM -05:00 local=11/1/2019 3:00:00 PM central=11/1/2019 10:00:00 AM eastern=11/1/2019 11:00:00 AM
in=11/5/2019 10:00:00 AM -06:00 local=11/5/2019 4:00:00 PM central=11/5/2019 10:00:00 AM eastern=11/5/2019 11:00:00 AM
in=11/1/2019 10:00:00 AM -04:00 local=11/1/2019 2:00:00 PM central=11/1/2019 9:00:00 AM eastern=11/1/2019 10:00:00 AM
using System;
using System.Linq;
public static class Program {
public static void Main() {
var zCentral = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var zEastern = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
foreach (var dto in new (int y, int m, int d, int h, double off)[]{
(2019, 11, 1, 10, -5),
(2019, 11, 5, 10, -6),
(2019, 11, 1, 10, -4),
}.Select(x=>new DateTimeOffset(x.y, x.m, x.d, x.h, 0, 0, TimeSpan.FromHours(x.off)))) {
var utcDt = dto.ToUniversalTime().DateTime;
// Note that local when run in sharplab.io is based on the server’s
// timezone which is hardcoded to UTC for this particular site.
Console.WriteLine($"in={dto} local={dto.LocalDateTime} central={TimeZoneInfo.ConvertTimeFromUtc(utcDt, zCentral)} eastern={TimeZoneInfo.ConvertTimeFromUtc(utcDt, zEastern)}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment