DateTime.ParseExact gives String was not recognized as a valid DateTime. DateTime.ParseExact gives String was not recognized as a valid DateTime. asp.net asp.net

DateTime.ParseExact gives String was not recognized as a valid DateTime.


You're using hh in your format string. That's a 12-hour "hour of day" field. The value 15 isn't in range...

You want HH instead, which is the 24-hour specifier.

See the MSDN custom date and time format strings documentation for more information.


Most probably due to the difference between your Server locale and the UI locale

One easier method will be to specify the globalization details in the web.config

like

<configuration>   <system.web>      <globalization culture="en-GB"/>   </system.web></configuration>

OR in more detail

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" />

But be sure this wont clash with your application in general


I am not sure if this helps but I used the exact code from this article and it worked for me because the DateTime.ParseExact(dat, "dd/MM/yyy HH:MM", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None) did not work for me.

Read this I just posted it online:http://rochcass.wordpress.com/2012/08/27/error-string-was-not-recognized-as-a-valid-datetime-solution/#more-350