27

I am getting to know NodaTime and like it a lot. But I don't know it that well (yet)!

Given a value such as '2014-04-08T09:30:18Z', what are the steps required to parse such a string to a NodaTime Instant?

Thank you!

ivnext
  • 887
  • 7
  • 15

1 Answers1

36

I figured this out. For others who want to do the same thing, here is what I used:

var isoString = "2014-04-08T09:30:18Z";

var result = InstantPattern.GeneralPattern.Parse(isoString).Value;

The Value property in this case returns the actual Instant object. If you omit that, the result is of type ParseResult<Instant> in this case, and has other information such as whether the parsing succeeded, etc.

http://nodatime.org/1.2.x/api/html/T_NodaTime_Text_ParseResult_1.htm

There aren't a lot of examples on Noda Time yet, but I am really liking it and turning to it more and more. Fantastic work by the team who created it. Thank you!

Zoe
  • 27,060
  • 21
  • 118
  • 148
ivnext
  • 887
  • 7
  • 15
  • 7
    Glad you got there - http://nodatime.org/1.2.x/userguide/text.html is the starting point for text handling in the user guide. I'd suggest using `InstantPattern.ExtendedIsoPattern` instead personally, as that will support fractional seconds too. You're right that we definitely need examples in the user guide... – Jon Skeet Apr 24 '14 at 09:50
  • 1
    @JonSkeet - Hi Jon. As someone who is also trying to adopt this library, thank you for your hard work. I must mention, as constructive criticism, that the documentation would be much easier to understand if some simple code samples were interwoven into it. – CShark Jul 07 '15 at 16:00
  • 1
    @CraftBeerHipsterDude: Understood. I'd really *like* to get a live experiment environment option via the code powering http://csharppad.com, but haven't managed to yet. For 2.0 we'd like to have a set of "recipes" too - I've started on it (http://nodatime.org/unstable/userguide/recipes.html) but haven't made much progress yet. – Jon Skeet Jul 07 '15 at 16:09
  • 2
    This does no longer work. `GeneralPattern` has been renamed to `Pattern`: `var result = InstantPattern.General.Parse(isoString).Value;` – Julien Ambos Aug 18 '20 at 15:43