How to convert string to datetime - dd/MM/yyyy

Hi all,

I have a datarow which is row(“Value”)
It is a string data type. it holds date which is MM/dd/yyyy format. Now I have been trying to convert the format of the date which will be dd/MM/yyyy. but I could not able to do it.

I have been trying Cdate(row(“Value”)).tostring(dd/MM/yyyy) but it is not working.
also I have tried -DateTime.ParseExact(row(“Value”).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

but it is showing error as it is a string data.

can you please help me how I can covert the date format successfully.

thanks in advance.

Hi @roysupriya21 ,

Could you let us know what is the Error Message received ?

Also, Perform a Debug and check what is the date value at that current iteration using Immediate Panel.

This should give us an idea of why the error happens.

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

Hi @roysupriya21

Can you try this-

string dateString = row[“Value”].ToString(); // Convert the value to a string

if (!string.IsNullOrEmpty(dateString))
{

DateTime date = DateTime.ParseExact(dateString, "MM/dd/yyyy", CultureInfo.InvariantCulture);

string formattedDate = date.ToString("dd/MM/yyyy");

msgbox formattedDate;

}
else
{

msgbox "Date value is null or empty."

}

Thanks!!

Hi @roysupriya21
Try this
Convert.ToDateTime(CurrentRow(“Value”).ToString).Date.ToString(“dd/MM/yyyy”)

I hope it will helps!!

Regards,

First you need to get the string out of the datatable. Assuming you’re looping through it with For Each Row in Datatable, you get the value with…

CurrentRow(“Value”).ToString

Now you need to convert that to a date. Best way is with ParseExact because it lets you designate the expected format. This is how to use ParseExact:

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

“MM/dd/yyyy” is the expected format of yourValue. Since the value you want is CurrentRow(“Value”).ToString just put that into the ParseExact:

DateTime.ParseExact(CurrentRow(“Value”).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

That gives you a datetime value. Remember, datetimes don’t have formats. You format them when you output them as a string so we need to output the above expression as a string with .ToString:

DateTime.ParseExact(CurrentRow(“Value”).ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

The .ToString method accepts a format inside parentheses, so that’s what I added on the end.

No its not working. still getting MM/dd/yyyy format date.

It’s Working for me
image

@roysupriya21
Can you show your code

@roysupriya21
Try this

  1. Add an Assign activity and set the value of the “dateValue” variable:

scssCopy code

dateValue = row("Value").ToString()
  1. Add another Assign activity to convert the date format:

makefileCopy code

parsedDate = DateTime.ParseExact(dateValue, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
  1. Add a final Assign activity to store the formatted date:

makefileCopy code

formattedDate = parsedDate.ToString("dd/MM/yyyy")

That’s a lot of extra work you don’t need to do. That can all just be one statement.