0

I am getting this exception:

String was not recognized as a valid DateTime

for this string:

2012-10-03T10:41:22.988401+01:00

using this code:

DateTime.ParseExact(TheStringAbove, "o", CultureInfo.InvariantCulture, DateTimeStyles.None);

I know that the DateTime string is UTC. Is there anything wrong with the code? Thanks.

cs0815
  • 16,751
  • 45
  • 136
  • 299

1 Answers1

7

That string is not UTC - it's explictly got something saying it's an hour ahead of UTC! That's what the +01:00 means. You really need to think about that part carefully.

The reason it's failing is that doesn't quite conform to the "o" format:

The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for DateTime values

Note that there are 7 fs there, but you've only got 6 decimal places. 2012-10-03T10:41:22.9884010+01:00 works fine - but you'll still need to check whether it actually means what you want it to mean, based on your expectation that this is really UTC.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks. The generated string is beyond my control ... so I have to ask the data provider to fix this. – cs0815 Oct 03 '12 at 10:03