I've found how to turn a DateTime into an ISO 8601 format, but nothing on how to do the reverse in C#.
I have 2010-08-20T15:00:00Z
, and I want to turn it into a DateTime
object.
I could separate the parts of the string myself, but that seems like a lot of work for something that is already an international standard.
This works fine in LINQPad4:
This solution makes use of the DateTimeStyles enumeration, and it also works with Z.
This prints the solution perfectly.
DateTime.ParseExact(...)
allows you to tell the parser what each character represents.Here is one that works better for me (LINQPad version):
produces
Although MSDN says that "s" and "o" formats reflect the standard, they seem to be able to parse only a limited subset of it. Especially it is a problem if the string contains time zone specification. (Neither it does for basic ISO8601 formats, or reduced precision formats - however this is not exactly your case.) That is why I make use of custom format strings when it comes to parsing ISO8601. Currently my preferred snippet is:
If you don't mind parsing TZ-less strings (I do), you can add an "s" line to greatly extend the number of covered format alterations.