How can I parse a string that lacks delimiters to a DateTime using c#?
Hey. I have, somehow, this string available "20100205 162206". This is a date and time without any delimiter char.
I need this back as a DateTime开发者_运维技巧 in C#. What is the best way?
Use one of the overloads of DateTime.ParseExact and specify a custom DateTime format string:
DateTime.ParseExact(
"20100205 162206",
"yyyyMMdd HHmmss",
CultureInfo.InvariantCulture);
What this does is specify an exact format string for your input. (Namely "year-month-day hour-minute-second" without the dashes.)
If your input will always come in in one way, you are safest to use the ParseExact function, because, if you recieve bad data, it allows you to "fail early" rather than operating on inconsistent data.
精彩评论