Parsing DateTime string using NodaTime

纵然是瞬间 提交于 2019-12-24 23:23:27

问题


How can we parse a Zoned DateTime string using NodaTime? I am currently using LocalDateTimePattern for parsing, but I think that the DateTime value yielded would be in the Server's timezone.

var pattern = LocalDateTimePattern.CreateWithInvariantCulture(dateTimePattern);

var parseResult = pattern.Parse(dateTimeString);
if (!parseResult.Success)
{
   // throw an exception or whatever you want to do
}

I came across ZonedDateTime pattern when I was looking at the Noda Time API. But, I am not able to use it. Am I missing out something?


回答1:


You don't say which version of Noda Time you're using, but I suspect it's 1.1.0 (that is, the latest released version).

ZonedDateTimePattern is only available in the development version of Noda Time (which will become 1.2.0 when it's released); as mentioned in the roadmap, 1.2.0 will include better text handling, including parsing for ZonedDateTime and OffsetDateTime.

I'm afraid that in 1.1.x there's no way to parse a ZonedDateTime directly (as noted in the limitations section of the 1.1.x user guide), though you could parse an Instant and a timezone name separately, and construct a ZonedDateTime by hand.



来源:https://stackoverflow.com/questions/16829460/parsing-datetime-string-using-nodatime

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