I want a calendar with only 7 days visible.
The days ['Ma', 'Di', 'Woe', 'Do', 'Vrij', 'Za', 'Zo']
must stay in the same position. Only the numbers may change.
My JS code doesn’t work.
<div class="calendar">
<p class="month" id="month"></p>
<div class="d-flex justify-content-around" id="daysContainer">
</div>
</div>
function generateWeekDays() {
var daysOfWeek = ['Ma', 'Di', 'Woe', 'Do', 'Vrij', 'Za', 'Zo'];
var today = new Date();
var currentDay = today.getDay();
var daysContainer = document.getElementById('daysContainer');
daysContainer.innerHTML = '';
for (var i = 0; i < 5; i++) {
var dayIndex = (currentDay + i) % 7;
var dayName = daysOfWeek[dayIndex];
var dayNumber = today.getDate() + i;
var dayElement = document.createElement('div');
var classes = 'flex-direction-row ' + dayName.toLowerCase();
if (i === 0) classes += ' active';
dayElement.className = classes;
dayElement.innerHTML = '<div class="number">' + dayNumber + '</div>'
+ '<div class="day">' + dayName + '</div>';
daysContainer.appendChild(dayElement);
}
var monthElement = document.getElementById('month');
var monthNames = ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'];
var currentMonth = today.getMonth();
monthElement.textContent = monthNames[currentMonth];
}
window.onload = generateWeekDays;
If I use console.log
then I see nothing in my console..
I don’t know what I doing wrong??
2
Answers
You’re looping only 5 times instead of 7, which means you’re not generating all days of the week.
Try this
I hope this helps
Something like that ?