Reputation: 1
There is a table with data and dates. Please help me to make a correct condition so that when it is triggered, the date will be coloured in different colours. The table has a Deadline column. I need the date to be colored red 5 days before the deadline, and gray after the date has passed. In other cases it is black. All in all I'm not really getting it right. The date in the column is written in this order 2023-02-01
moment(currentRow["Deadline"]).fromNow() >= "in 5 days"?'#f50202' : moment(currentRow["Deadline"]).fromNow() > "in 0 days"?
'#0d0d0d' : '#999999'
Tried working with a simple condition if the deadline date is less than the current text is greyed out. I can't find a solution to the more complex condition yet.
Upvotes: 0
Views: 509
Reputation: 147343
moment's fromNow method returns a string, so you are comparing strings, not relative times.
The >= operator will compare the strings character by character until it reaches different characters, then returns the result of that comparison. So "2 hours ago" >= "1 year ago"
will return true when you might expect it to return false. Similarly "in 5 minutes" >= "in 4 years"
will also return true.
What you need is something like:
moment(currentRow["Deadline"]) >= moment().add(5, 'days')
or, if you want to just compare the date and ignore time:
moment(currentRow["Deadline"]) >= moment().add(5, 'days').startOf('day')
Note that if currentRow["Deadline"]
does not resolve to a format understood by moment.js, you should also include the format so that it is parsed correctly, otherwise it will fall back to implementation dependent heuristics for parsing.
Upvotes: 1