Parsing MMddyyyy or ddMMyyyy Date Format

Today I was discussing how to convert date strings MMddyyyy (e.g. 03282007) or ddMMyyyy (e.g. 28032007) back to DateTime object.

Ok, this is not as straightforward as it looks. For example, for any DateTime objects, you can use .ToString(“MMddyyyy”) to convert it to a string format. But you can’t parse that string back using DateTime.Parse() method.

One obvious way to parse such string is to use SubString method to extract the three parts of the date. As it turned out, this conversion could be done easier using DateTime.ParseExact():

string s = “03292007”;
DateTime dt = DateTime.ParseExact(
s1, “MMddyyyy”, CultureInfo.CurrentCulture);

Be Sociable, Share!

One Comment

  1. pmortlock says:

    Works great thanks Kerry

Leave a Reply