String comparison bug

I have an expression tag that makes a string comparison to determine if the current time is before or after a certain time my expression is:

dateFormat(now(),"HH:mm") < "21:30"

and I noticed a strange behavior when the time is from 21:00 to 21:30

so I made some tests and I simplified the expression to this simple comparison:

"21:13" < "21:30"

And the result is False, instead of true.

You’re comparing strings when you really want to be comparing datetimes.
So instead of converting the current time into a string, you should convert today’s 21:30 into a datetime.

now() < setTime(now(), 21, 30, 0)

As @bmusson mentioned, you’re doing a comparison on Strings here, and the expression language will attempt to coerce these to numbers before giving up all together.

In this case your comparison ends up being 21.0 < 21.0, resulting in false.

hummm, ok, that’s kind of a gotcha.

Thanks a lot Kevin and Ben

1 Like