Convert string to DateTime not working

Hello to all,

I have been reading all I could find in this forum about converting strings into datetime, but none of the threads answer this question.

I am reading a excel file and there is a column with date. This date, tho, can rather be a string or a date, and to check if is a date I use IsDate. However, when it is not a date, and i am trying to convert it to date, i ALWAYS encounter some kind of error.

Sometimes CDate works, and sometimes I get a “Cannot assign CDate(x) to date”. Then I use TryParse, and it works. However, sometimes TryParse does not work and I get the same error but with TryParse, or ParseExact.

Is like I cannot seem to find a ONE and specific formula to just convert any kind of string into date!! Could someone help me please?

Kind regards,

Laura.

Use Convert.ToDateTime(String)

Regards,
Saloni

Hello @sb001,

Thanks for the quick reply. Already try it and I get the same error: Cannot assign Convert.ToDateTime(x) to date.

hi @l.sestafes

Var_date = Datetime.ParseExact(“12/05/2022”,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

where Var_date is a variable of type datetime

Can you share your Excel File?

Hi @l.sestafes
Have a look here for some steps when doing an analysis:

:ambulance: :sos: [FirstAid] Datatable: Debug & Analysis invalid DateTime Strings / String to DateTime Parsing Issues - News / Tutorials - UiPath Community Forum

As mentioned the values within the datatable are not mandatory the same as we do see in the excel file. But as described you can analyise and check.

In case of your date date looks like a number then check following:
grafik

Hello @nikhil.girish,

Right now this formula is working. I hope it works with several string formats! We will see, but at least, it is working now. Thank you very much!

1 Like

Hello to all,

So, I tried this formula, which gives a date variable:

Var_date = Datetime.ParseExact(“12/05/2022”,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

However, this works when the input from the Input file is a date, and not when it’s a string. UiPath seems to get the Date column value as a string or date depending on how the user filled it in.

Then, there is this formula, which gives a string variable instead of a date:

var_string2 = Convert.ToDateTime(var_string1).ToString(dd/MM/yyyy)

And what’s the problem with that one? Well, when the input is a date, then it doesn’t work. The error in both formulas is the same: “Cannot assign XXX to XXX”

So, what I have finally done is:

var_date = Convert.ToDateTime(Convert.ToDateTime(var_string1).ToString(dd/MM/yyyy))

May not be the best, but it is working for both strings and dates!

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