开发者

DateTime.ParseExact not working at all, why?

开发者 https://www.devze.com 2023-01-25 13:01 出处:网络
I am attempting to parse the following String into a DateTime object in c#: DateTime.ParseExact(\"20101108 230125\", \"yyyyMMdd hhmmss\", null)

I am attempting to parse the following String into a DateTime object in c#:

DateTime.ParseExact("20101108 230125", "yyyyMMdd hhmmss", null)

although th开发者_如何转开发e value looks correct the ParseExact method just keeps giving me the following:

String was not recognized as a valid DateTime.

Can anybody tell me why and how I can parse the above string without having to do it the manual way? Isn't ParseExact supposed to be for this kind of occasion?


You got the format for hours wrong, should be uppercase:

DateTime.ParseExact("20101108 230125","yyyyMMdd HHmmss", null)

Lowercase hh specifies that the time uses a 12-hour clock (with AM/PM). Uppercase HH is a 24 hour clock time.

For detailed info, check the documentation of custom DateTime format strings.


Try using:

var dt = DateTime.ParseExact("20101108 230125", "yyyyMMdd HHmmss", null)

The "hh" is for 12 hour time and "HH" for 24 hour.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号