Skip to content

Instantly share code, notes, and snippets.

@justinsaraceno
Last active January 24, 2024 22:23
Show Gist options
  • Save justinsaraceno/c89923ab2374b41c7069 to your computer and use it in GitHub Desktop.
Save justinsaraceno/c89923ab2374b41c7069 to your computer and use it in GitHub Desktop.
Convert UTC DateTime to a specified time zone using Noda Time
// the time zone to convert to
var timeZone = DateTimeZoneProviders.Tzdb["US/Eastern"];
// the date as UTC - this could be from a data store
var fakeUtcDate = new DateTime(2015, 11, 01, 07, 30, 00);
// convert to instant from UTC - see http://stackoverflow.com/questions/20807799/using-nodatime-how-to-convert-an-instant-to-the-corresponding-systems-zoneddat
var instant = Instant.FromDateTimeUtc(DateTime.SpecifyKind(fakeUtcDate, DateTimeKind.Utc));
var result = instant.InZone(timeZone).ToDateTimeUnspecified();
@satheesh-krishnasamy
Copy link

Thank you. This answers my search..

@andrewletskalyuk
Copy link

Thank you Justin

@ckyongrtl
Copy link

Thanks! Nice snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment