I am trying to parse this datetime, but it always return false.
DateTime.TryParseExact("07/01/2007 12:15", "mm/dd/yyyy HH:mm", new CultureInfo("en-US"), DateTimeStyles.None, out met)
I am trying to parse this datetime, but it always return false.
DateTime.TryParseExact("07/01/2007 12:15", "mm/dd/yyyy HH:mm", new CultureInfo("en-US"), DateTimeStyles.None, out met)
The pattern for month is capital MM
:
"MM/dd/yyyy HH:mm"
mm
stands for minutes and you've already used it at the end.
The problem is that at runtime it finds two components of minutes in the given string as specified by the format for parsing. So you're not able to construct a valid DateTime object from the given input-string with format specified. It finds 07
and 15
both as minutes hence the problem.
When you run the code with ParseExact
and without TryParse, you'll get following exception.
System.FormatException: DateTime pattern 'm' appears more than once with different values.
The Solution: Note that, mm
is for minutes, MM
is for months. In your particular case, you need to tell which part is the month and which one is the minutes. Assuming you need 07
as the month, Here's the corrected version of your code.
DateTime.TryParseExact("07/01/2007 12:15", "MM/dd/yyyy HH:mm", new CultureInfo("en-US"), DateTimeStyles.None, out met)