Reputation: 5137
I have a string 20100524 (2010 05 24) and I would like to parse it as an actual date format.
Upvotes: 3
Views: 413
Reputation: 4745
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "20100524";
string format = "yyyyMMdd";
result = DateTime.ParseExact(dateString, format, provider);
Upvotes: 3
Reputation: 4169
DateTime.ParseExact("20100524", "yyyyMMdd", Thread.CurrentThread.CurrentCulture);
Upvotes: 3
Reputation: 158309
This will do it for you in a safe manner:
DateTime dateTime;
if (DateTime.TryParseExact("20100524", "yyyyMMdd", null, DateTimeStyles.None, out dateTime))
{
// use dateTime here
}
else
{
// the string could not be parsed as a DateTime
}
Upvotes: 16