String is not recognized as a valid datetime - MM/dd/yyyy format is only working

Hi,

I am trying to get the string as Datetime format from an excel row.
But, bot is taking first one as month and second one as Date. Due to that bot is throwing error as “string is not recognized as a valid datetime”.

Datetime_variable = DateTime.ParseExact(Datetime123,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Datetime123 Value is : 18/4/2022 2:35:13 AM . From here I only need Date, could someone help me on this.

1 Like

Convert.todatetime(Var).Date

Or if you just need it as a string then use var.Split(" "c).First

to convert to datetime
dateTimevar = DateTime.ParseExact(dateTime123,"d/M/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture)

get date
dateTimevar.tostring(“dd/MM/yyyy”)

@maddy99

Welcome back to our UiPath community.

Try below expression.


DateTime.ParseExact(Datetime123,{“dd/MM/yyyy hh:mm:ss tt”,"dd/M/yyyy hh:mm:ss tt"},System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Hi

Welcome back to uipath forum

If this is the date format in ur file

Then the below expression would help you resolve this issue

Datetime.ParseExact(Datetime123.ToString.SubString(0,10).Trim, {“dd/M/yyyy”, “dd/MM/yyyy”}, System.Globalization.CultureInfo.InvariantCulture)

For more details on Datetime refer this thread

Cheers @maddy99

Hi @Palaniyappan ,

Which variable type should be assigned to the above code, as I used DateTime variable and also string but getting this error.

image

@maddy99

try this instead
Datetime.ParseExact(Datetime123.SubString(0,10).Trim, "d/M/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

this code can be assigned to string

image

1 Like

Fine

If you want Datetime as a output variable then mention like this in assign activity

datetimevar = Datetime.ParseExact(Datetime123.ToString.SubString(0,10).Trim, {“dd/M/yyyy”, “dd/MM/yyyy”}, System.Globalization.CultureInfo.InvariantCulture)

Where datetimevar is a variable of type System.Datetime

Or

If you want as a string output

stroutput = Datetime.ParseExact(Datetime123.ToString.SubString(0,10).Trim, {“dd/M/yyyy”, “dd/MM/yyyy”}, System.Globalization.CultureInfo.InvariantCulture).ToString

Cheers @maddy99

1 Like

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