简体   繁体   中英

UTC string to DateTime exception

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM