tags:

views:

81

answers:

2

How do I convert a string in ddMMyyyy format to a DateTime?

+2  A: 

See Parsing Date and Time and DateTime.ParseExact()

String dateString = "15072008";
String format = "ddMMyyyy";
try {
   DateTime result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
   Console.WriteLine("{0} is not in the correct format.", dateString);
}

Prints:

15072008 converts to 7/15/2008 12:00:00 AM.
NullUserException
more specifically, `DateTime.ParseExact()`
Mark
You could also use `TryParseExact()` instead of catching an exception.
Callum Rogers
+10  A: 

Try using DateTime.ParseExact:

DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);
Callum Rogers