Can't do JS Date Object Comparison in Python Code

I noticed when trying to compare two JS Date objects in an Anvil form with a >/</>=/<= operands, that I get an error about that not being valid.

This doesn’t happen if I run the comparison either in the Browser Console or directly in script tags, so I know it’s not a JS flaw.

Not a huge deal, but still seems like a bug.

App showing the JS success and Form failure:
https://anvil.works/build#clone:7F45AHFBVJA3AF3L=XIQLZ2NYBORPYNBS745T3ZN5

P.S. I’m aware of Python’s Datetime library’s date object and I used this as a workaround, but that meant I had to use both Python and JS Date objects, which makes my code less consistent. (The Javascript component I’m integrating unfortunately requires the JS Date object for time calculations and wouldn’t accept the python date object :frowning: )

JavaScript wrapped objects won’t support the Python comparison operators. That’s because comparison in the two languages is very different. In Python comparison will fail loudly if it’s not sensible, in Javascript it will always succeed even when it’s not sensible. And there’s no way of knowing if you’re comparing “sensible” objects or not. e.g. if you try comparing a Date object to "foo" in the browser console it will succeed.

Searching for comparing Dates in Javascript suggests comparing the return value of the .getTime() method, which will return an integer timestamp - ideal for comparison in Python.

1 Like

Oh gosh, this is where the friction between JS and Python really comes in, lol! Thanks for explaining!

1 Like