Strange behavior adding days to Now()

I know there are differences in how Lucee and ACF handle dates, so I wanted to run this by everyone to see if this behavior in Lucee is by design or a bug.

In ACF, “now() + 1” returns tomorrow’s date, so I can do <CFSET my_date = now() + 1> and my_date will be tomorrow’s date. However, in Lucee, the same code sets my_date to a decimal number (e.g. 44857.852011759256). This means that a date comparison such as <CFIF the_date LT (now() + 1)> that works in ACF does NOT work in Lucee because in Lucee it’s comparing a date to a decimal number instead of two dates like it does in ACF.

I know the workaround is to use dateAdd instead of adding or subtracting directly from now(), HOWEVER, DateFormat(now() + 1) actually works in Lucee as it does in ACF and returns a date. I don’t understand why now() + 1 works inside the DateFormat function, but does not work in a CFSET or CFIF statement. Any insight would be greatly appreciated.

Thanks
Rich

Adding numbers to dates actually works the same across engines. This code

writedump( now()+0 )

currently outputs 44859.149601516205 on trycf.com for both engines, which I believe is the number of days since Jan 1st 1900. The difference between Lucee and Adobe is that Lucee will ONLY do explicit date comparisons when at least one side of the operation is an actual date object, or you use a proper date function. This is the same reason the following code

writedump( '1/1/2022' > '2/1/2021' )

returns YES for Adobe and false for Lucee. This is a known and documented difference which is done for performance. All the behind the scenes attempts to cast every single string and number to a date costs a price Lucee isn’t willing to pay.

There are a few ways to turn a numeric date into a proper date object, but none of them seem any better than just using a date function.

To be clear, this WILL work if the_date contains a “proper” date object. If the_date is a string or number, it will not work like you expect.

Yes, because you’ve used an explicit date function so Lucee casts it for you.

Technically, it returns a string, not a date.

1 Like

Thank you so much. I had a feeling the behavior was due to implicit date conversions, but I couldn’t put my mind around it until I read your post. I really appreciate the detailed explanation.

Thanks,
Rich

2 Likes