Hi any idea how to bind every day of a week to every h1 in a way the first h1 shows today, and the next h1 shows tomorrow and the 7th h1 shows the 7th day from toady ? I did this my max was I can only make all of them show toady.. thanks
JavaScript
x
25
25
1
function currentdaychange() {
2
3
let weekday = ['Sunday',
4
'Monday',
5
'Tuesday',
6
'Wednesday',
7
'Thursday',
8
'Friday',
9
'Saturday'
10
]
11
[new Date().getDay()];
12
13
console.log(weekday)
14
15
let items = document.querySelectorAll('.currentday');
16
17
document.querySelector('.currentday').innerHTML = weekday;
18
19
for (var i = 0; i < items.length; i++) {
20
items[i].innerHTML = weekday;
21
}
22
requestAnimationFrame(currentdaychange);
23
}
24
25
currentdaychange();
JavaScript
1
7
1
<h1 class=currentday></h1>
2
<h1 class=currentday></h1>
3
<h1 class=currentday></h1>
4
<h1 class=currentday></h1>
5
<h1 class=currentday></h1>
6
<h1 class=currentday></h1>
7
<h1 class=currentday></h1>
Advertisement
Answer
You can retrieve a localized day of the week using Date.prototype.toLocaleDateString
and passing in the option { weekday: 'long' }
.
JavaScript
1
17
17
1
const formatDayOfWeek = date =>
2
date.toLocaleDateString('en-US', { weekday: 'long' });
3
4
const incrementDay = date => {
5
date.setDate(date.getDate() + 1);
6
return date;
7
};
8
9
const currentDayChange = () => {
10
const now = new Date();
11
[document.querySelectorAll('.day-of-week')].forEach(day => {
12
day.textContent = formatDayOfWeek(now);
13
incrementDay(now);
14
});
15
}
16
17
currentDayChange();
JavaScript
1
3
1
h1 {
2
font-size: smaller;
3
}
JavaScript
1
7
1
<h1 class="day-of-week current-day"></h1>
2
<h1 class="day-of-week"></h1>
3
<h1 class="day-of-week"></h1>
4
<h1 class="day-of-week"></h1>
5
<h1 class="day-of-week"></h1>
6
<h1 class="day-of-week"></h1>
7
<h1 class="day-of-week"></h1>