How to convert date format 2/19/2023 using Uipath

Hi Team,

I have different types of date formats are extracted by Bot. But here I need to convert to M/d/yyyy dare format.

Input date format will be any one of the above attachment date format.so I need to convert the date in to M/d/yyyy.

For example input date is 19 Feb 2023 or 02/19/2023 or 19/02/2023 or 19.02.2023 or 02.19.2023 or 2.19.2023 or 19.2.2023 or 19/2/2023.

Output will be 2/19/2023.

How to change the date format M/d/yyyy based on input date format.

Kindly suggest

Hi @Baby123

Can you try the below

DateTime.ParseExact(InputDate,{"dd MMM yyyy","MM/dd/yyyy","dd/MM/yyyy","dd.MM.yyyy","M.dd.yyyy","dd.M.yyyy","dd/M/yyyy"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("M/dd/yyyy")

Regards,

@Baby123

Cheers!!

Hey @Baby123
try this
If(DateTime.TryParseExact(inputDateString, New String() {"d MMM yyyy", "dd/MM/yyyy", "d.MM.yyyy", "MM.dd.yyyy", "M.d.yyyy", "d.M.yyyy", "dd.M.yyyy", "d/MM/yyyy", "dd.MM.yyyy"}, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, New DateTime()), DateTime.ParseExact(inputDateString, New String() {"d MMM yyyy", "dd/MM/yyyy", "d.MM.yyyy", "MM.dd.yyyy", "M.d.yyyy", "d.M.yyyy", "dd.M.yyyy", "d/MM/yyyy", "dd.MM.yyyy"}, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None).ToString("M/d/yyyy"), "Invalid Date").ToString()
`

Hi @Baby123

Try this:

str_Date = DateTime.ParseExact(currentText.ToString.Trim(),{"d MMM yyyy","M/d/yyyy","d/M/yyyy","d.M.yyyy","M.d.yyyy"},System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None).ToString("M/d/yyyy")

str_Date is of DataType System.String

image

The above syntax will accept the formats of dates "d MMM yyyy", "M/d/yyyy", "d/M/yyyy", "d.M.yyyy", "M.d.yyyy".

Please let me know if you have any other formats, I will give the changed syntax.

Hope it helps!!

Beware that if the day and month of original date are both not greater than 13, such as 2.4.2024, the first convertible format will be applied.
In above expression, “d.M.yyyy” is met first and the output will be Apr 02, 2024.
What if the original date is presenting 2024 Feb 04?

If the array syntax is changed to {“d MMM yyyy”, “M/d/yyyy”, “d/M/yyyy”, “M.d.yyyy”, “d.M.yyyy”}, “M.d.yyyy” is met first and the output will be Feb 04, 2024.
Wondering any solutions on this.

Hi @healsko_ho

Specifying that many formats within an array is also not an good practice. Rather than that we can go with a single date format and parse it to the required date format right.

As per given input I have give the syntax, if the user has that format of dates also, then all the required formats must be given.

Regards

Hi @lrtetala ,
I have one doubt like for example my input date format will be current date 01/03/2023. How Bot will know which date format it is because 01 and 03 month number are available right.

How Bot will know which is the month and Day.

Kindly suggest

@Baby123

In this case you have to give like below

DateTime.ParseExact(Input,"dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture).ToString("M/dd/yyyy")

I asking like how bot will identify whether the 01 is day and 03 is month.

If 01/03/2024 date like this
Is that possible like month 01 and date is 03 right

In this scenario bot did wrong right?

How to handle this kind of scenarios. Kindly suggest

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