Append a dynamic stylesheet to the document head:
const today = new Date();
const today_date = today.getDate();
const days = [...Array(today_date).keys()].splice(1).map(
day => `#day-${day}`
).join(',');
const styles = `
#day-${today_date} {
font-weight: bold;
color: green;
}
${days} {
color: silver;
}
`;
var styleSheet = document.createElement("style");
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
<div id="day-18" class="something">content</div>
<div id="day-19" class="something">content</div>
<div id="day-20" class="something">content</div>
<div id="day-21" class="something">content</div>
<div id="day-22" class="something">content</div>
<div id="day-23" class="something">content</div>
<div id="day-24" class="something">content</div>
<div id="day-25" class="something">content</div>
<div id="day-26" class="something">content</div>
Starting by instantiating a Date object, assigning it to variable today
, then using it's method getDate() to assign the current day number of the month to variable today_date
.
Next, instantiating an array with that number of elements using Array object and providing today_date
for it's parameter, then get it's key values using it's keys() method, along with the spread operator and array literal notation to populate the array. keys()
provides an array of numbers, 0
through yesterday's day number, so splice() the array starting from the second element, index 1
, to the end, which results in an array containing day numbers 1
through yesterday.
The array chain is continued by map'ing it to string interpolate the day numbers (keys) into an array of strings containing "#day-" and the day number. Now join'ing this array of strings into a single, plain string, then interpolating it into the variable styles
.
Finally create a stylesheet element using createElement() and append it to the head using appendChild().