Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string in ddMMyyyy format to a DateTime

Tags:

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

like image 773
Dan Avatar asked Sep 23 '10 16:09

Dan


Video Answer


2 Answers

Try using DateTime.ParseExact:

DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);
like image 132
Callum Rogers Avatar answered Sep 29 '22 13:09

Callum Rogers


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.
like image 40
NullUserException Avatar answered Sep 29 '22 12:09

NullUserException