System.DateTime.ParseExact : unrecognized format string

.net, c#, datetime, iso8601, timestamp

Solution

The problem is that `YYYY` and `DD` are not a valid format specifiers. Use `yyyy` and `dd` instead, and if you want to use 24-hour time, use `HH`:

var timestamp = DateTime.ParseExact("20140101T000000Z", "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture);

Also note that the `Z` here is interpreted as a literal `Z` character. It's not a substitute for `z`, the timezone specifier.

Further Reading

- Custom Date and Time Format Strings

Problem

``` var timestamp = DateTime.ParseExact("20140101T000000Z", "YYYYMMDDThhmmssZ", CultureInfo.InvariantCulture); System.FormatException was unhandled by user code HResult=-2146233033 Message=String was not recognized as a valid DateTime. Source=mscorlib ``` This makes no sense to me as `YYYYMMDDThhmmssZ` is ISO-8601 `YYYY-MM-DDThh:mm:ssZ` with the special formatting characters removed.

Original source