Create a DateTimeOffset set to Midnight in a different TimeZone from server

戏子无情 提交于 2019-12-12 13:28:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!