Table Text Color Date Comparison

I have a table and would like to compare a date field in the record to today's date to set the color and am coming up with an issue.
most dates work fine, some don't.
using this code in the text color box of that field:
image
I get these results:
image
I can't for the life of me figure out why both of the date values individually come out correct but they can't seem to accurately compare.

Hi @Shegs,

Maybe try adding another columns or 2 to the table with the value of {{ new Date().toLocaleDateString() }} and the result of the other columns you're comparing to help debug what's going on?

Today is generated by:
image

What I am looking for is for the Expiration Date to be red if it is in the past.
currently it appears to work for some but not all fields.

Hello @Shegs and @MikeCB!

With Retool we use the Javascript library Moment.js for generating date objects, and this library has a ton of amazing methods that can help make operations using dates a breeze!

For this use case, I would recommend the .isBefore() method that comes built in to the Moment.js Date() class object. Alternatively there is also an .isAfter() method you can use, depending on how you want to set up the boolean logic.

I tested with the below example and it worked for properly comparing the dates and coloring the rows, let me know if this works for you!

{{ moment(yourTable.data[i].WCExpirationDate).isBefore(moment()) ? 'Red' : 'Blue'}}

Replace yourTable variable with the name of the table you are using :sweat_smile:

The i variable will be created as the table iterates over each row and will be able to properly access the row, as a better alternative to 'currentSourceRow'.

I saw you were keying in to "WCExpirationDate", you should be able to access this data as a property of the row currently being iterated over with dot notation.

Side note, the method .toLocaleDateString() will return a string which can be tricky to compare with Javascript and might have been the reason for the odd behavior you were seeing!

1 Like

That did work, thanks.

as for the keying, I was just trying different ways to get to the data. I trend towards dot notation first because it's more simple.

Thanks
-Aaron