I have a string like this:
"20090212"
and I want to convert to valid C# datetime.
Do I need to parse it out because that seems too much work?
I have a string like this:
"20090212"
and I want to convert to valid C# datetime.
Do I need to parse it out because that seems too much work?
You can use DateTime.ParseExact:
DateTime result =
DateTime.ParseExact("20090212", "yyyyMMdd", CultureInfo.InvariantCulture);
DateTime.ParseExact(str, "yyyyMMdd", CultureInfo.CurrentCulture);
... and I really doubt I got there first.
Although for completeness, I prefer TryParseExact
DateTime dt;
if(DateTime.TryParseExact(str, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out dt)) {
// ... use the variable dt
}