дата плюс месяц, равно не более последнего числа след месяца
как исправить функцию, при клике на кнопку +месяц, должно переходить к соответственной дате следующего месяца, но если такой даты нету (31янв + мес =31янв), то должно переходить к последней дате следующего месяца (28/29 февраля)
помогите плиз исправить
<!DOCTYPE html>
<html>
<head>
<title>„tear-off calendar“</title>
<style>
div {
white-space: nowrap;
}
input {
text-align: center;
}
input,
select {
background-color: hsl(210,100%,90%);
color: hsl(210,100%,40%);
border: 1.5px solid hsl(210,100%,55%);
border-radius: 15px/12px;
font: bold 18px serif;
padding: 2px 5px;
margin-bottom: 5px;
outline: none;
}
</style>
</head>
<body>
<div>
<select id="weekday"></select>
<button id="minusweek">-</button>
<button id="plusweek">+</button><br>
<input id="desiredday" type="text" size="1">
<button id="minusday">-</button>
<button id="plusday">+</button><br>
<select id="desiredmonth"></select>
<button id="minusmonth">-</button>
<button id="plusmonth">+</button><br>
<input id="desiredyear" type="text" size="4">
<button id="minusyear">-</button>
<button id="plusyear">+</button><br>
<select id="desiredera"></select>
</div>
<br>
<span id="rezult"></span>
<script>
var elementweekday = document.getElementById('weekday');
var elementmonthday = document.getElementById('desiredday');
var elementmonth = document.getElementById('desiredmonth');
var elementyear = document.getElementById("desiredyear");
var elementera = document.getElementById("desiredera");
var days = ["воскресенье", "понедельник", "вторник", "среда", "четверг", "пятница", "суббота"];
var months = ["января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря"];
var eras = [["до н. э.", "-"], ["н. э.", "+"]];
function formatDate(date) {
var yyyy = date.getFullYear();
var dd = date.getDate();
if (dd < 10) {
dd = "0" + dd;
}
return days[date.getDay()] + "<br>" + dd + " " + months[date.getMonth()] + " " + yyyy + " г.";
}
function updateDate() {
var weekdayIndex = elementweekday.selectedIndex;
var year = elementyear.value.trim();
var eraIndex = elementera.selectedIndex;
if (eraIndex === 0 && year === "1") {
year = "0000";
} else if (eraIndex === 0 && year > 1 && year <= 999999) {
year = "-" + (parseInt(year, 10) - 1).toString().padStart(6, "0");
} else if (eraIndex === 1 && year >= 1 && year <= 9999) {
year = year.toString().padStart(4, "0");
} else if (eraIndex === 1 && year >= 10000 && year <= 999999) {
year = "+" + year.toString().padStart(6, "0");
}
var month = (elementmonth.selectedIndex + 1).toString().padStart(2, "0");
var day = elementmonthday.value.trim().padStart(2, "0");
var dateString = year + "-" + month + "-" + day;
var date = new Date(dateString);
elementweekday.innerHTML = "<option>" + days[date.getDay()] + "</option>";
var formattedDate = formatDate(date);
document.getElementById("rezult").innerHTML = formattedDate;
}
elementweekday.addEventListener("change", updateDate);
elementmonthday.addEventListener("keyup", updateDate);
elementmonth.addEventListener("change", updateDate);
elementyear.addEventListener("keyup", updateDate);
elementera.addEventListener("change", updateDate);
var currentDate = new Date();
// День недели
elementweekday.appendChild(new Option(days[currentDate.getDay()], days[currentDate.getDay()], true));
// Число месяца
elementmonthday.value = currentDate.getDate();
// Месяц
for (var i = 0; i < months.length; i++) {
var option = new Option(months[i], i);
if (i === currentDate.getMonth()) option.selected = true;
elementmonth.appendChild(option);
}
// Год
elementyear.value = currentDate.getFullYear();
// Эра
for (var i = 0; i < eras.length; i++) {
var opt = elementera.appendChild(new Option(eras[i][0], eras[i][1]));
if (eras[i][0] === "н. э.") opt.selected = true;
}
// Обновить дату при нажатии на кнопки
document.getElementById("minusweek").addEventListener("click", function() {
currentDate.setDate(currentDate.getDate() - 7);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("plusweek").addEventListener("click", function() {
currentDate.setDate(currentDate.getDate() + 7);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("minusday").addEventListener("click", function() {
currentDate.setDate(currentDate.getDate() - 1);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("plusday").addEventListener("click", function() {
currentDate.setDate(currentDate.getDate() + 1);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("minusmonth").addEventListener("click", function() {
currentDate.setMonth(currentDate.getMonth() - 1);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("plusmonth").addEventListener("click", function() {
currentDate.setMonth(currentDate.getMonth() + 1);
if (currentDate.getMonth() !== (currentDate + 1) % 12) {
currentDate.setDate(0);
}
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("minusyear").addEventListener("click", function() {
currentDate.setFullYear(currentDate.getFullYear() - 1);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
document.getElementById("plusyear").addEventListener("click", function() {
currentDate.setFullYear(currentDate.getFullYear() + 1);
elementweekday.selectedIndex = currentDate.getDay();
elementmonthday.value = currentDate.getDate();
elementmonth.selectedIndex = currentDate.getMonth();
elementyear.value = currentDate.getFullYear();
updateDate();
});
updateDate();
</script>
</body>
</html>