Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with DateTime.ParseExact method: always throws exception even with correct format

I have searched the forum for solutions like this, however I did not find one that actually matches my particular issue.

This might be in need of a more experienced eye in order to find the problem, so I appreciate all help!

Problem: I am trying to parse a string with a date to a DateTime variable. However, even if the string date format is exactly the same, it still throws an exception.

I would like to know why, and how I can solve it. I really can't see what is wrong there!

try
{
   string value = "Sep-17-2012 03:04:07 am";

   string format = "M-dd-yyyy hh:mm:ss tt";

   DateTime temp = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
}
catch(Exception e){}

Thanks in advance,

Mad

like image 869
MadGatsu Avatar asked Dec 27 '22 04:12

MadGatsu


1 Answers

Your format should be MMM not M http://www.dotnetperls.com/datetime-format

string format = "MMM-dd-yyyy hh:mm:ss tt";

M - display one-digit month number

MMM - display a three letter month

like image 78
Anthony Russell Avatar answered Apr 27 '23 11:04

Anthony Russell