Compare to Time Value

studio
comparison
timespan

#1

Hi,

I would like to compare current time with a fix time value. How we I can achieve this in UIpath
For Example
fixed_time = 16:00:00;
current_time = 17:30:00;
I want to check->
If(fixed_time>current_time)
{
///Action A
}
else{
Action B
}


#2

Ensure that your variables are in the datetime format, and you can use an if activity (or a decision if using a flowchart).


#3

Hi there @anshul_nema,
There’s a few ways you can write this, but the below should do the trick:

tspFixed = 17:30:00
If DateTime.Now.TimeOfDay.CompareTo(tspFixed) = -1 
* Log - "Local Time Is Greater Than Fixed Time!"
Else
* Log - "Local Time Is Less Than Or Equal To Fixed Time!"
End If

Thanks in advance,
Josh


#4

Hi Mr_JDavey,

Thanks for providing this solution.
I may be wrong, but I think you have it backwards.

When current time is greater than fixed time,
DateTime.Now.TimeOfDay.CompareTo(tspFixed) outputs a 1

and when current time is less than fixed time,
DateTime.Now.TimeOfDay.CompareTo(tspFixed) outputs a -1

Best,
Ian


#5

Hi there @in006,
You are absolutely correct, apologies!

For anyone visiting this page, please see the below:
Less than zero

  • This instance is earlier than value.

Zero

  • This instance is the same as value.

Greater than zero

  • This instance is later than value.

Take from:
https://msdn.microsoft.com/en-us/library/5ata5aya(v=vs.110).aspx

Thanks in advance,
Josh