Lakshman
Lakshman

Reputation: 65

Parsing DateTime string using NodaTime

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?

Upvotes: 1

Views: 1443

Answers (1)

Malcolm Rowe
Malcolm Rowe

Reputation: 777

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.

Upvotes: 1

Related Questions