Comparing Times

Hi!

I am working on a workflow that needs to perform a set of actions depending on the time of day. I have the Get Current Date activity with quite a few variables that extract the month, day, year, time, etc. For the current_time variable, I use the following moment js logic:

moment(current_date).format(‘LT’)

This seems to capture the time in the format that I need. I can either use the 12-hour with AM/PM or the 24-hour. Either would be fine for what I need.

I added an If…Then activity with the following logic, but it doesn’t seem to be working.

(current_time >= “7:00 AM” && current_time < “9:00 AM”)
((current_time >= “7:00 AM”) && (current_time < “9:00 AM”))

No matter what time it is or how I manipulate the above logic, the If…Then always goes to the No branch.

Thank you in advance for any guidance you can provide!

Brandon

I copied and pasted your lines and it worked just fine for me. Can you spot any important differences besides the time?

I used (current_time >= “3:00 PM” && current_time < “4:00 PM”).

Thank you for your response! I have been noticing that it either works or it doesn’t, but I couldn’t identify the cause. That said, I just played around with the times and noticed something. It’s after 3 PM local time so I used a simpler logic, current_time > ‘3:00 PM’. This worked, so I went back to using current_time > ‘7:00 AM’. Again, it didn’t work, so I tried hour by hour. It wasn’t until I adjusted the time to 10:00 AM that it worked again. The only difference I could see was that there are 2 digits for the hour. I didn’t think that would be the issue since we both have been using single digits; however, maybe for the AM it does? When I ran the workflow again using ‘07:00 AM’ as the time, it worked.

Well, the variable “current_time” is a string. When you use the operators <>= for strings, the result considers alphabetic order for letters and the first digit for numbers. So 10 starts with 1, which is less than 7, therefore the bot understands that “10” < “7”. On the other hand, 07 starts with 0, which is less than 1, therefore “10” > “07”.

I’m pretty sure that’s the reason why the code works with two digits, but not with one. You would have to convert the string into type date and work with that in order to avoid that problem. I find it useful to use miliseconds when working with dates. It’s more accurate and precvents that sort of confusion.