I am making a site that gets data from a speedrun.com API and puts the newest runs on the site in order of time. I recently added a feature where runs from the same day that the site is viewed will have a special icon showing that they are "NEW". This is my current solution (Javascript):
runs[i].date = "2021-12-12"; // The API does not return hours
if (new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toJSON().slice(0, 10) === runs[i].date) {
alert("Today");
}
If the date was December 12th, 2021, this would alert Today
.
This solution accounts for timezone differences and works well. However, I would like this to be possible for runs that happened in the last 3 days, so even if runs[i].date = "2021-12-11"
(December 10th) or runs[i].date = "2021-12-11"
(December 11th), it would still alert Today
.
How would this be done?