4

I have a server set to EST and am wondering what I need to do to create a DateTimeOffset set to Midnight of the current day but in a different timezone? For example PST?

Chris
  • 1,006
  • 10
  • 13

1 Answers1

6
  • Fetch the relevant TimeZoneInfo.
  • Construct a DateTime which contains the local time (i.e. midnight)
  • Call TimeZoneInfo.GetUtcOffset to find the offset from UTC
  • Construct a DateTimeOffset with the local time and the offset

(If Noda Time were production-ready, I'd suggest creating a ZonedDateTime using that, but unfortunately we're not even close to ready for v1 yet... although actually the bits you'd need are probably stable enough :)

It's worth noting that "midnight of the current day" doesn't always exist in all time zones. If you may need to deal with time zones which have a DST transition at midnight, you may want to look at TimeZoneInfo.IsAmbiguousTime and TimeZoneInfo.IsInvalidTime.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194