A client is sending a string containing a date in format YYYYMMDDHHmmSS
(e.g. 201004224432
). There are no separators like /
or -
.
How can I easily convert this to a DateTime object? Convert.ToDateTime()
does not work.
A client is sending a string containing a date in format YYYYMMDDHHmmSS
(e.g. 201004224432
). There are no separators like /
or -
.
How can I easily convert this to a DateTime object? Convert.ToDateTime()
does not work.
Use DateTime.ParseExact:
var date = DateTime.ParseExact(
"201004224432",
"yyyyMMddHHmmss",
CultureInfo.InvariantCulture);
Note the tweaks to your format string to work appropriately.
You want DateTime.ParseExact
, which can take in a formatting string like yours and use it to parse the input string.