Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ParseExact not working at all, why?

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?

like image 559
William Calleja Avatar asked Nov 16 '10 08:11

William Calleja


2 Answers

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.

like image 54
Fredrik Mörk Avatar answered Sep 23 '22 06:09

Fredrik Mörk


Try using:

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

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

like image 44
Richard Smith Avatar answered Sep 25 '22 06:09

Richard Smith