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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With