DateTime.ParseExact doesn't work at all, why?

I am trying to parse the following String DateTime object in C #:

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

although the value looks correct, the ParseExact method just gives me the following:

The string was not recognized as a valid DateTime.

Can someone tell me why and how can I parse the above line without doing it manually? Is ParseExact for such a case?

+6
c # datetime date-format
source share
2 answers

Your format is incorrect, it should be uppercase:

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

Lower case hh indicates that the time uses a 12-hour clock (with AM / PM). Upper case hh is a 24-hour time.

For more information, check out the documentation for the documentation for custom DateTime format strings .

+21
source share

Try using:

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

β€œhh” for 12 hours and β€œHH” for 24 hours.

+2
source share

All Articles