I wanted to make a function that changes a li element in such a way that it shows the date of the next week every time the Next week button is pressed but I don't know how to check if a new month has started using the setDate() method.
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
const elemen = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh"];
var weeknumber = 0;
function setweek() {
let d = new Date();
const start = d.getDate();
for (let i = 0; i < 7; i++) {
d.setDate(start + i)
document.getElementById(elemen[i]).innerHTML = days[d.getDay()] + '<br>' + d.getDate() + "." + (d.getMonth() + 1);
}
}
function addweek() {
weeknumber++;
let d = new Date();
const start = d.getDate();
for (let i = 0; i < 7; i++) {
d.setDate(start + i + (weeknumber * 7));
// In this code I need a if function too check if a month has passed so it sets the weeknumber to 0.
document.getElementById(elemen[i]).innerHTML = days[d.getDay()] + '<br>' + d.getDate() + "." + (d.getMonth() + 1);
}
}
<body onload="setweek();">
<label for="weekly_meal_plan">Weekly meal plan:</label><br>
<ul class="list-group" id="weekly_meal_plan">
<li id="first" class="list-group-item list-group-item-action"></li>
<li id="second" class="list-group-item list-group-item-action"></li>
<li id="third" class="list-group-item list-group-item-action"></li>
<li id="fourth" class="list-group-item list-group-item-action"></li>
<li id="fifth" class="list-group-item list-group-item-action"></li>
<li id="sixth" class="list-group-item list-group-item-action"></li>
<li id="seventh" class="list-group-item list-group-item-action"></li>
</ul>
<br>
<button id="nweek" onclick="addweek();">Next week</button>
</body>