Skip to content

Instantly share code, notes, and snippets.

@rianjs
Last active April 18, 2018 15:29
Show Gist options
  • Save rianjs/8e42e89357645b587938b22dad60e86b to your computer and use it in GitHub Desktop.
Save rianjs/8e42e89357645b587938b22dad60e86b to your computer and use it in GitHub Desktop.
NodaTime bread and butter examples
// Create the WND time with a real time zone
var localDt = LocalDateTime.FromDateTime(DateTime.Now);
var wndTz = DateTimeZoneProviders.Tzdb["America/Toronto"];
var zonedAsWnd = localDt.InZoneLeniently(wndTz);
// Convert that WND time to RNO time
var renoTz = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
var inReno = zonedAsWnd.WithZone(renoTz);
// Convert a DateTimeOffset to WND time with a real time zone
var inThePast = DateTimeOffset.Now.AddMonths(-6);
var wndFromDto = Instant.FromDateTimeOffset(inThePast).InZone(wndTz);
// Convert that zoned WND time to VEN time
var venloTz = DateTimeZoneProviders.Tzdb["Europe/Amsterdam"];
var venlo = wndFromDto.WithZone(venloTz);
// And convert it back to DateTimeOffset
var venloOffset = venlo.ToDateTimeOffset();
// And to a DateTime, which will have a DateTimeKind of Unspecified (rather than Local)
var venloDt = venlo.ToDateTimeUnspecified();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment