May 10, 2011

String was not recognized as a valid DateTime

Error:
The message of exception is "String was not recognized as a valid DateTime."

Problem:
- I am trying to convert my string formated value to date type with DateTime.ParseExact but ...
- I get the error saying "String was not recognized as a valid DateTime" when I used of ParseExact ...
- ... in some value as "9/7/2008 13:00:00" when im going to set startdate it throw was not recognized as a valid DateTime and ...

Solution:
The DateTime.ParseExact have not any problem and the orginal problem comes from us thus you attention to String of Formatx
EXAMPLE: the string format for "02/Dec/2007:14:15:40 -08:00" is
WRONG: dd/MMM/yyyy:hh:mm:ss zzz
RIGHT: dd/MMM/yyyy:HH:mm:ss zzz

Did you got the problem source! It comes from (hh) and because the hour is greater than 12 need the (HH). Ok, look at again to the StringFormat and debug it again ;-)

No comments: