javascript days keep moving over 31 days a month

As the guys on the comments suggested, I tried using libraries (dayjs to be exact) but couldn’t figure out how to, as I haven’t learned that yet.
And as per @Teemu suggestion (with a little modification for the whole week) I used date.setDate(date.getDate()+i-1); then used day=date.getDate() for the day value, and it worked.
From a little search I’ve found that setDate method takes an integer of the ‘day’ of the month as an argument.
Ref: https://www.w3schools.com/jsref/jsref_setdate.asp
Final function looked like this:

window.addEventListener('load',function () {
    for (let i = 1; i < 8; i++) {
        let date = new Date();
        date.setDate(date.getDate()+i-1);
         let day = date.getDate(),
            month = date.getMonth() + 1,
            year = date.getFullYear();
        let days=["Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"];
        document.getElementById("day"+i).innerHTML=
            `${days[date.getDay()]} <br> ${day}/${month}/${year}`;
    }
})

Ps: I fixed the addEventListener('onload',function(){}) problem by using load for the type argument, thanks to @Reyno for the clarification.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top