问题
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?
回答1:
- 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.
来源:https://stackoverflow.com/questions/4668390/create-a-datetimeoffset-set-to-midnight-in-a-different-timezone-from-server