I’m trying to parse the following string to a DateTime:
“04-03-2008-16-18-08”
To simplify things I’ve written this unit test which always throws the exception “System.FormatException: String was not recognized as a valid DateTime.”
[TestMethod]
public void TemporaryDateTimeParseTest()
{
DateTime.ParseExact("04-03-2008-16-18-08", "dd-MM-yyyy-hh-mm-ss", null);
}
The format should be day-month-year-hour-minute-second (as should be evident from above). It seems right according to the MSDN description to me. What am I missing?
I have seen people say ParseExact is awkward, if this is the wrong method to use how can I use the regular Parse method to pick up this format?
You need to use
HHfor a 24-hour clock instead ofhhfor 12-hour clock.MSDN Custom Date and Time Format Strings