Date conversion from string to date

Hello

If I have a string of 16/01/2020 (within a variable) does anyone know how to convert it to read as a datetime variable as 16/01/2020?

Hi @Bob2,

Try Convert.ToDateTime(YourDateVar)

Cheers

Thanks @YAZIDI but uipath doesn’t seem to recognize dates in the format of dd/MM/yyyy and can not do a simple conversion. That would only work if my string date was MM/dd/yyyy

Try this instead:

Date.ParseExact(YourDateVar, “dd/MM/yyyy”,System.Globalization.DateTimeFormatInfo.InvariantInfo)

Thanks - I tried putting that into a DateTime variable but still getting an error

image

:face_with_raised_eyebrow: that strange its working for me, maybe the format is incorrect try use this format: MM/dd/yyyy instead of dd/MM/yyyy

maybe you have some white spaces in your string, try using Trim before the conversion to date…

Where am I going wrong?

Variable type String
image

Variable type DateTime
image

The first date is a print of ‘RecievedDate’ variable

The second date is a print of ‘endDate’ variable. I need it to read as 16/01/2020

image

Can you try this?
DateTime.ParseExact(yourDate, “dd/MM/yyyy”, CultureInfo.InvariantCulture)

1 Like

in here you can see it is not validated, maybe your quotes cannot be copied and pasted from here, delete them and type again…

DateTime variable always contains the Full date, you cannot cut it, but what you can do is read it as string like so: endDate.ToString("dd/MM/yyyy")

1 Like