Compare two dates in a data table

Hello,

i want to compare two columns in one data table which contains dates.
e.g.
Column1 Column2
02.01.22 03.05.22
12.02.20 05.03.22

I tried it in a for each loop with currentrow.item(“column1”).toString <> currentrow.item(“column2”).toString
It doesnt work properly. The code shows me also equal date values.

we would recommend to debug and to inspect the values. Also use the different debugging panels for the analysis

Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

https://www.google.com/search?q=uipath+compare+two+datatables+site:forum.uipath.com&safe=active&rlz=1C1GCEA_enUS956US956&sa=X&ved=2ahUKEwif96br3q_3AhVwTt8KHWYnC24QrQIoBHoECA4QBQ

You don’t compare dates as strings.

Datetime.ParseExact(currentrow.item(“column1”).toString,“MM.dd.yy”,System.Globalization.CultureInfo.InvariantCulture) <> Datetime.ParseParseExact(currentrow.item(“column2”).toString,“dd.MM.yy”,System.Globalization.CultureInfo.InvariantCulture)

Note that I’m not sure the formats MM.dd.yy and dd.MM.yy are correct as I can’t tell is 03.05.22 is March 5 2022 or May 3 2022.

Hi @Schlosser_Tobias you can try this, first try to parse the date using Datetime.ParseExact(currentrow.item(“column1”).toString,“dd.MM.yy”,System.Globalization.CultureInfo.InvariantCulture) <> Datetime.ParseExact(currentrow.item(“column2”).toString,“dd.MM.yy”,System.Globalization.CultureInfo.InvariantCulture) and place this in IF condition

Attaching for your reference:
CompareDates.zip (9.3 KB)

Thanks,
Parth Modi

Hi @Schlosser_Tobias ,

As you are comparing the date values to be Equal or Not, we could go with the String Comparison and Check if they are Equal provided they are in the same formats.

Hence, Inspect the date value from the row item at first. There might be a possibility that the Time also gets displayed or present in the value separating the date value with a space.

Next, For Comparison of only date values, we can use the below Expression :

Split(currentrow("column1").ToString)(0).Trim <> Split(currentrow("column2").ToString)(0).Trim

Do note that above String comparison on date values can be performed only when the operation is to Check Equal or Not Equal, else we would require to Convert the String date value to DateTime and then Compare

How i can convert it into datetime?

Ways to convert strings to datetime variable:

  1. Convert.ToDateTime(stringVar)
    eg. Convert.ToDateTime(“02/15/2022”)
  2. Datetime.ParseExact(stringVar,format of stringVar,System.Globalization.CultureInfo.InvariantCulture)
    eg. Datetime.ParseExact(“02/15/2022”,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Ok thank you,

i have figured out that i have in column1 the values from type date and in column2 type String. If i convert the date type into string it seems to be the best way. But there is another problem, that i have in some rows from column1 no value and i got on this point an exception.

@Schlosser_Tobias ,

Could you Provide us the Expression/Method that gives out the Exception ?

I use an assign activity inside the for each loop:
CurrentRow.Item(“Column1”) = convert.ToDateTime(CurrentRow.Item(“Column1”)).ToString(“dd.MM.yyyy”)

@Schlosser_Tobias ,

Maybe the Below Updated Statement will help Resolve the Exception :

CurrentRow("Column1") =If(String.IsNullOrWhiteSpace(CurrentRow("Column1").ToString),"", Convert.ToDateTime(CurrentRow("Column1")).ToString("dd.MM.yyyy"))

Let us know if you still receive the Exception or it doesn’t work as expected.

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