Parsing datetime from String
asp.net, c#
Solution
Your format is specifying time in 24 hours format, but your string has time in 12 hour format. For parsing time your format should be `"hh:mm tt"`
String a = "04/23/2014 12:00 AM";
DateTime dt = DateTime.ParseExact(a, "MM/dd/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);
You should see: Custom Date and Time Format Strings
HH --- The hour, using a 24-hour clock from 00 to 23. More information: The "HH" Custom Format Specifier.
hh --- The hour, using a 12-hour clock from 01 to 12. More information: The "hh" Custom Format Specifier.
Problem
I have a string with ``` String a= "04/23/2014 12:00 AM" DateTime.ParseExact(a, "MM/dd/yyyy HH:mm tt", System.Globalization.CultureInfo.InvariantCulture); ``` This Shows an error System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.ParseExact