문제

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

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

although the 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top