Assign : String was not recognized as a valid DateTime to DateTime variable

Reading an excel file and storing the date as a date time variable however I am getting an error for this date “7/2/2022” in the excel file. The format is dd/MM/yyyy.

When the robot reads in the cell it prints it as, “07/02/2022” 00:00:00.
image

Code Here:

Datetime.ParseExact(rDetail(“Date”).ToString,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

@Nelson.R

Try below expression.

Datetime.ParseExact(rDetail(“Date”).ToString,{"dd/MM/yyyy","d/M/yyyy"},System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

The issue is that your string “07/02/2022 00:00:00" is not recognized. You can replace “00:00:00” by empty string and try again.

Try the expression below.

Datetime.ParseExact(rDetail(“Date”).ToString.Replace(“00:00:00”,“”).trim,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Hi @Nelson.R ,

If the formats are all dd/MM/yyyy and maybe some have the time formats as well.

Then maybe we can only consider the first half of the Date value by Split.

We can Check if the Below Works :

Datetime.ParseExact(Split(rDetail("Date").ToString.Trim)(0).Trim,"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture)

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