Cast date/time string to datetime variable

I have a column of String variables in the following format: “dd-MMM-yyyy hh:mm AM/PM” (e.g. 17-MAR-2021 11:50 AM and 17-MAR-2021 01:17 PM). I need to cast this to a datetime variable.

I tried Datetime.ParseExact(row.Item(“VersionDate”).ToString,“dd-MMM-yyyy HH:mm”,System.Globalization.CultureInfo.InvariantCulture) but am getting an error that the string is not recognized as a valid datetime. Wondering if it has to do with the AM/PM at the end… any thoughts would be appreciated!

Please try this…

Datetime.ParseExact(row.Item(“VersionDate”).ToString,“dd-MMM-yyyy hh:mm tt”,System.Globalization.CultureInfo.InvariantCulture)

Please find the reference for the datetime formats below…

thank you! that worked perfectly.

1 Like

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