Parsing DateTime AM/PM Problem

Hi. I have a string with the format “22/02/2019 - 07:24 PM”.
For which I’m using the format “dd/MM/yyyy - HH:mm tt”

But it throws “string not recognized as valid datetime” when I try to convert that string into a datetime, like this:
Datetime.ParseExact(myString, “dd/MM/yyyy - HH:mm tt”, System.Globalization.CultureInfo.InvariantCulture)

I know for a fact that the “AM/PM” are the ones causing the problem, because if I don’t use them, the parse works.
But I do need the “AM/PM” becase I need to be able to differentiate the hour, which goes from 1 to 12 AM/PM. Is there a way to solve this problem?.

this will work

DateTime.ParseExact("22/02/2019 - 07:24 PM", "dd/MM/yyyy - hh:mm tt", nothing)

you need to use hh instead of HH because HH is 24 hour time which doesnt work with AM / PM e.g. 15:03 AM doesnt make sense but 03:03 AM or 03:03 pm makes sense

1 Like

Thank you, it works now. I didn’t know about that difference. This is the first time that I used AM/PM.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.