How to convert an inconsistent date

Hi,

I am scraping a date from a pdf received via email. Unfortunately the date format is either “D/MM/YYYY” or “DD/MM/YYYY”.

If the date scraped is “D/MM/YYYY” I would like to convert to “DD/MM/YYYY”. What would be the best way to do this please?

Thanks

2 Likes

I would check the length of the string until the first slash character and then append a zero to the start of the string if it’s only one digit long.

Try this out:
Strvariable.split("/"c)(0).length

1 Like

Fine
Hope these steps would help you resolve this
—let’s say you have your date in a string variable
Then use a assign activity like this
Yourstringvariable = Yourstringvariable.PadLeft(10,cchar(“0”))

This will directly add 0 to the front of date if we have only one character in date along the DateTime string

so if your input is “2/12/2019” the above expression will give us ouput as “02/12/2019”
And we don’t need to validate the length or check with that and it becomes straight forward that if has 10 character in total no worries or not will add a 0 in the beginning

—but make sure that month is mentioned in two characters always

Cheers @TRX

5 Likes

Thanks for the help. The issue I now face is that I need to convert the value back to a date variable as it is used to compare against another date. What would be the best way to do this please?

I’ve tried Convert.ToDateTime(EmailReceivedDate,Globalization.CultureInfo.InvariantCulture) but it didnt seem to work.

Thanks,

Hi @TRX

I would recommend when dealing with dates to parse and format it as a date, rather than using string manipulation.

You can use DateTime.TryParseExact() to check if the date is in a certain format.
Google .net DateTime.TryParseExact

Then, you can use DateTime.ParseExact() to convert the string to a DateTime type.
Google .net DateTime.ParseExact

Once it is in a DateTime type, then you can simply format it by adding .ToString("DD/MM/yyyy") on the end.

So, essentially (and assuming I do this right), you would do something like this with If activities:

If activity: condition => DateTime.TryParseExact(datestring, "D/MM/yyyy", Globalization.CultureInfo.InvariantCulture)
  True: Assign activity: datestring = DateTime.ParseExact(datestring, "D/MM/yyyy", Globalization.CultureInfo.InvariantCulture).ToString("DD/MM/yyyy")

Note: you only need to use ParseExact if your culture does not match the date format that you are trying to parse. For example, in US, dates are in the format where Month is before days. And, so CDate() or Convert.ToDateTime() in that case would work unless the days is before the Month.

Anyway, I hope this helps get you in the right direction. :smiley:

@TRX if you have any problems, post a snippet of your code and errors you are receiving, which will help identify the solution.

Regards. @chenderson @Palaniyappan

Clayton.

4 Likes

Hi Clayton,

Thanks a lot for your reply. I tried the following code in an IF statement:

DateTime.TryParseExact(DueDate, “D/MM/yyyy”, Globalization.CultureInfo.InvariantCulture)

Unfortunately it is producing the following error: “Overload Resolution Failed because no accessible ‘TryParseExact’ accepts this number of arguments.”

Did I structure the code correctly?

Sorry about that @TRX

As shown in the link I provided above and in UiPath when you type the parentheses, it was missing some arguments:


You need the styles and out reference.

You can just do this:
DateTime.TryParseExact(DueDate,"D/MM/yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, Nothing)

You might want to test this first though, because it kept giving me False.

Thanks Clayton