Make the hour format (H
) lowercase like this:
DateTime.ParseExact(
"4/4/2010 4:20:00 PM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Uppercase "H" indicates 24-hour time and lowercase "h" indicates 12-hour time and will respect the AM/PM in the candidate string.
Side note: It is best to provide an instance of IFormatProvider
to methods like this (even if it's just CultureInfo.InvariantCulture
). It's one of those things that doesn't really matter until you hit problems with it so it can be good to be in the habit of specifying culture information.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…