Compare to Time Value

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
}

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

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

2 Likes

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

2 Likes

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:

Thanks in advance,
Josh

1 Like

Hi all,

I want to compare current time with between 7 to 8am & 8am to 8pm.

Kindly suggest me how to make it happen