简体   繁体   English

UTC字符串到DateTime异常

[英]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. 我知道DateTime字符串是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! 该字符串不是 UTC,而是明确表示它比UTC提前一个小时! That's what the +01:00 means. 这就是+01:00的意思。 You really need to think about that part carefully. 您确实需要仔细考虑这一部分。

The reason it's failing is that doesn't quite conform to the "o" format : 失败的原因是不完全符合“ o”格式

The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for DateTime values “ O”或“ o”标准格式说明符对应于DateTime值的“ yyyy”-'MM'-'dd'T'HH':'mm':'ss..fffffffK“自定义格式字符串

Note that there are 7 fs there, but you've only got 6 decimal places. 请注意,那里有7 fs,但是您只有6个小数位。 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. 2012-10-03T10:41:22.9884010+01:00可以正常工作-但您仍然需要检查一下它是否真正意味着您想要的意思,这是基于您对这确实是UTC的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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