hh
specifier is for 01
to 12
.
Use HH
specifier which is for 00
to 23
. (24-hour clock based)
And I think you should use date
instead of test
in your DateTime.TryParseExact
method.
string date = "02/27/2014 23:00:28";
string pattern = "MM/dd/yyyy HH:mm:ss";
DateTime parsedDate;
bool test= DateTime.TryParseExact(date, pattern,
CultureInfo.InvariantCulture,
DateTimeStyles.None, out parsedDate);
Console.WriteLine(test); // True
Since you using null
in your IFormatProvider
parameter, it uses CurrentCulture
. From documentation;
If provider is null, the CultureInfo
object that corresponds to the
current culture is used.
But / format specifier
has a special meaning of "replace me with the current culture date seperator" in your string format.
That means, if your current culture's date separator is not /
, your parsing operation will be fail. That's why you should use InvariantCulture
in such a case.
Here an another answer: TryParseExact returns false, though I don't know why