Код: Выделить всё
newDate.setDate(newDate.getDate() + 7);
Код: Выделить всё
01/01/2025 + 0 days = 10/01/2025 = + 9 days = Wrong
01/01/2025 + 1 days = 11/01/2025 = + 10 days = Wrong
01/01/2025 + 9 days = 19/01/2025 = + 18 days = Wrong
01/01/2025 + 10 days = 20/04/2025 = + 109 days = Wrong
01/01/2025 - 0 days = 01/01/2025 = - 0 days = Correct
01/01/2025 - 1 days = 31/12/2024 = - 1 days = Correct
01/01/2025 - 9 days = 23/12/2024 = - 9 days = Correct
01/01/2025 - 10 days = 22/12/2024 = - 10 days = Correct
< /code>
Я создал простой пример, чтобы продемонстрировать проблему;
(заранее прошу прощения за то, насколько она ужасна, впервые используя JS) < /p>
var dateFormat = "dd/mm/yy";
$(document).ready(function() {
$("#AddBtn").click(function() {
var myDate = $.datepicker.parseDate(
dateFormat,
document.getElementById("V1_Date").value
);
var myDays = document.getElementById("DayRange").value;
myDate = addDays(myDate, myDays);
var formattedDate = $.datepicker.formatDate(dateFormat, myDate);
document.getElementById("ResultDate").value = formattedDate;
});
$("#SubBtn").click(function() {
var myDate = $.datepicker.parseDate(
dateFormat,
document.getElementById("V1_Date").value
);
var myDays = document.getElementById("DayRange").value;
myDate = subDays(myDate, myDays);
var formattedDate = $.datepicker.formatDate(dateFormat, myDate);
document.getElementById("ResultDate").value = formattedDate;
});
});
var xDate = $("#V1_Date").datepicker({
changeYear: true,
changeMonth: true,
dateFormat: dateFormat,
});
function addDays(date, days) {
const newDate = new Date(date);
newDate.setDate(newDate.getDate() + days);
return newDate;
}
function subDays(date, days) {
const newDate = new Date(date);
newDate.setDate(newDate.getDate() - days);
return newDate;
}< /code>
Days:
Date:
Result:
Подробнее здесь: https://stackoverflow.com/questions/796 ... cting-does
Мобильная версия