Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have an HTML table containing a list of due dates. I would like the background colour of the cell to change depending on the difference between the due date and today's date:

Black - due date has passed
Red - due date within 30 days
Orange - due date between 30 and 60 days
Green - due date more than 60 days away

From what I've read I could define a class within CSS, use getElementsByClassName in javascript to modify the colour, but how? And how do you calculate the date differences?

What I have tried:

I haven't tried anything worth mentioning because I am unsure how to proceed, hence the question.
F-ES Sitecore 26-Jun-20 9:43am
I'd create classes with semantic names, so have one called dueDatePassed, one called dueWithinThirty etc and attach the styles you want to them. That way if you want to change how they appear later on your class names still make sense. To find out how to add\remove classes from elements just google "javascript add remove class", it is well documented. Likewise google "javascript compare dates" to find out how to do that.

If it was me I'd attach a data attribute to the "td" element that contained the date in a known format, or maybe the ticks, for more robust converting into a date variable

<td data-date="2020-06-26">26th June 2020<td/>
W Balboos, GHB 2-Jul-20 8:31am
Maybe you're looking in the wrong spot.

Where do these dated come from that fill your table? If it's a database you have the ability to do this server-side.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100