How to compare time in UiPath

Hi Team,
I have a datatable with the column name as “time”.
Where time is 12 hours format and AM/PM is mentioned in the excel file.

For e.g. 2:45 PM, 11 AM like this.

So here I want to do comparison using filter data table activity. However can’t filter it out, as it has AM and PM mentioned in the column name.

How to solve this issue? Let’s us consider, current time is 3PM and I was latest messages which are within 30 mins scope.
So if “time” column has value 2:45 PM, that it should be kept in the datatable. Any insight would be appreciated.

I using following condition in filter data table :
“time” < System.DateTime.Now.ToString("hh:mm”)

@ PrankurJoshi
@Palaniyappan

Thanks,
Shantanu

Hi @Shantanu_More1

Firstly you can try to convert the format like

DateTime.Parse("02:45 PM");

Then you can simply put your filter and it should work.

Thanks,
Prankur

Hi @Shantanu_More1 ,

We could use the Linq Method to Filter out the rows based on Date Values.

However if the format of the Time appears in the quoted format above, we might need to perform a String Substitution/Replace on . before proceeding with Parsing of the String.

The Expression to Filter out the rows using a Linq would be like below :

ArrayRow = DT.AsEnumerable.Where(Function(x)DateTime.TryParse(x("time").ToString.Replace(".",":"),new Date) andAlso Math.Abs(DateDiff(DateInterval.Minute,DateTime.Parse(x("time").ToString.Replace(".",":")),Now))<30).ToArray

Where ArrayRow is a variable of Type Array of DataRow and DT is input Datatable variable.

After the Above Assignment, we can then Check in a if Condition If there are any Rows in ArrayRow like below :

ArrayRow.Any

If the Condition is True use an Assign Statement and Convert the ArrayRow into a Datatable.

OutputDT = ArrayRow.CopyToDatatable //Assign Statement

The Above steps are done to handle cases when there are no rows after filtering.

Also, if the Condition for Time Comparison is such that the time in Datatable should always be less than Current time, then use the below Linq Query :

ArrayRow = DT.AsEnumerable.Where(Function(x)DateTime.TryParse(x("time").ToString.Replace(".",":"),new Date) andAlso DateDiff(DateInterval.Minute,DateTime.Parse(x("time").ToString.Replace(".",":")),Now)>0 andAlso DateDiff(DateInterval.Minute,DateTime.Parse(x("time").ToString.Replace(".",":")),Now)<30).ToArray

Let us know if doesn’t work or if you need alternate solutions.

Hi supermanPunch, Thanks for the reply. I’m sorry for the typo:
Time is in the format of 2:45 PM, there is “:”

Also, I have another question. If time column from the datable is not in date time format, than how would this expression work?

We, first might need to convert into date time format(It’s in General Format in excel) and need then need to compare right?
And, I trying to solve this using “filter data table activity”. As I have lots of other condition for other columns as well.

Thanks,
Shantanu

@Shantanu_More1 , This is the approach we do normally for these kind of situations.

The Filter Datatable Activity might not be able to perform the comparison in the right way.

You Could try First Converting the Value in Time Column to a TimeSpan format by adding Another Column of TimeSpan Type to the Datatable.

Then you would need to update the New Column by Converting the Value in Time Column to Timespan and assigning it to the new Column.

Now, Most probably after the Updation of the new Column, we should be able to perform a Comparison between TimeSpan Column created and Now.TimeOfDay using a Filter Datatable Activity.

Since, these are all Extra Steps required for Filter Datatable Comparison, we go with the Linq Approach.