本文介绍了为什么DateTime.ParseExact解析UTC格式与尾随Z?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个ParseExact问题。我正在尝试将UTC格式的字符串解析为datetime,格式为:



YYYY-MM-DDThh:mm:ss。 ssZ



这是UTC格式,尾随Z.由于某种原因我无法解析。我已经尝试过u,s,o自定义格式的字符串,以及几个DateTimeStyles以及手写。文化是不变的。



由于某种原因,它不喜欢Z,表示它是UTC字符串。当我删除它,解析。我会高兴地,有一些满足,摆脱尾随Z,因为我知道他们是UTC datetime字符串,但我不能。任何想法?

解决方案

您提到的格式字符串是。您应该使用,如下所示:

  DateTime.ParseExact(2011-03-02T20:15:19.64Z,yyyy-MM-ddTHH:mm:ss.ffK ,null).ToUniversalTime()

如果你不把 ToUniversalTime ()结束时,结果将转换为您当地的时区。


Another ParseExact problem. I'm trying to parse a UTC formatted string to a datetime with the format of:

"YYYY-MM-DDThh:mm:ss.ssZ"

which is in UTC format, with the trailing Z. I can't parse exact it for some reason. I have tried the "u", "s", "o" custom format strings, as well as as several DateTimeStyles and well as handwritten. The culture is invariant.

For some reason it doesn't like the Z, which indicates it's a UTC string. When I remove it, parses. I would happily, with some satisfaction, get rid of the trailing Z as I know they are UTC datetime strings, but I can't. Any ideas?

解决方案

The format strings you mentioned are standard format strings. You should probably use a custom format string, like this:

DateTime.ParseExact("2011-03-02T20:15:19.64Z", "yyyy-MM-ddTHH:mm:ss.ffK", null).ToUniversalTime()

If you don't put ToUniversalTime() at the end, the result will be converted to your local time zone.

这篇关于为什么DateTime.ParseExact解析UTC格式与尾随Z?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 04:28