DateTime.Parse DD / MM / YYYY 24-hour clock

DateTime.Parse fails when

15/08/2000 16:58 

Any thoughts?

I need to parse the dates and get some international.

DD / MM / YYYY will not fail until the 24-hour hour

MM / DD / YYYY will take 24-hour time

08/15/2000 4:58 PM will understand

From Kibby's answer, I looked at other cultures. I use Regex to determine if it is dd / MM, and if so use the fr-FR culture.

+7
source share
2 answers

Try DateTime.ParseExact() :

 var result = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm", new CultureInfo("en-US")); 
+26
source

You should probably use DateTime.ParseExact to parse the date if you know the exact format you expect from the date. For your purposes, the following is likely to work.

 string dateString, format; DateTime result; CultureInfo provider = CultureInfo.InvariantCulture; dateString = "15/08/2000 16:58" format = "dd/MM/yyyy HH:mm" result = DateTime.ParseExact(dateString, format, provider); 

Go to the next one. Changed hh to HH because HH means 24 hour time. If you are not using a leading zero, just use H. For more information on creating format strings, see this article .

Also from a related MSDN article, it seems that the "g" format should work.

 dateString = "15/06/2008 08:30"; format = "g"; CultureInfo provider = new CultureInfo("fr-FR"); DateTime result = DateTime.ParseExact(dateString, format, provider); 
+11
source

All Articles