i want to compare two columns in one data table which contains dates.
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.
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)
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:
- Datetime.ParseExact(stringVar,format of stringVar,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.
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”)
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.