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
2010-09-23 16:46:46
more specifically, `DateTime.ParseExact()`
Mark
2010-09-23 16:48:05
You could also use `TryParseExact()` instead of catching an exception.
Callum Rogers
2010-09-23 16:56:50
+10
A:
Try using DateTime.ParseExact
:
DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);
Callum Rogers
2010-09-23 16:49:51