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?
Asked
Active
Viewed 2,294 times
1 Answers
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
-
Interesting. Can you give an example of a timezone that skips over midnight during a transition? While it's possible, I assumed (probably incorrectly) that this didn't happen in the real world. – Matt Johnson-Pint Jan 11 '13 at 00:42
-
I found one. Brazil. DateTime.Date - Crash! OUCH! – Matt Johnson-Pint Jan 11 '13 at 01:15
-
@MattJohnson: Yup, Brazil's the one I'm aware of. There may be other ones too. – Jon Skeet Jan 11 '13 at 06:35