How to convert specific string to Date Time?

Hi all,

I have a string variable lastUpdated(Dec 12, 2023 4:02 PM) that needs to be converted to Date Time format (dd/mm/yyy/hh:mm).
I tried with method DateTime.ParseExact(lastUpdated, “dd-MM-yyyy”,System.Globalization.CultureInfo.InvariantCulture) but getting error: Assign: String was not recognized as a valid DateTime.

Should I use different method in order to achieve convertion to Date Time?

Thank you!

dd-MM-yyyy doesn’t match your string. That format has to match your string.

Use…

MMM dd, yyyy h:mm tt

Hi,

In this case, CDate also works.

image

CDate( lastUpdated).ToString("dd/MM/yyyy HH:mm")

Regards,

1 Like

Hi @bp777

You are getting the error because the format given in the string variable and the format in the DateTime conversion does not match. Check out the syntaxes given below. This will provide you the correct output.

lastUpdated= "Dec 12, 2023 4:02 PM"
ConvertedDate= DateTime.ParseExact(lastUpdated, "MMM dd, yyyy h:mm tt", System.Globalization.CultureInfo.InvariantCulture)

Hope it helps!!

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