Answer the question
In order to leave comments, you need to log in
How to get dates with an interval of a month from a given one?
There are two parameters: the date of reference (04/08/2022) and the number of months / repetitions of the cycle (120). You need to get the corresponding array of dates.
let countMonth = 120,
currDate = '08.04.2022',
str;
for (var i = 1; i <= countMonth; i++) {
str += currDate; // вот тут и проблема как получить +1 месяц каждый цикл, при кратности 12 ( if (i % 12 == 0) ) +1 год и месяц снова на 1 поставить и так 120 раз...
}
Answer the question
In order to leave comments, you need to log in
function getDates(startStr, count) {
const dates = [];
const date = new Date(startStr.split('.').reverse().join('-'));
const day = date.getDate();
date.setDate(0);
for (let i = 0; i < count; i++) {
date.setMonth(date.getMonth() + 2, 0);
date.setDate(Math.min(date.getDate(), day));
dates.push(date.toLocaleDateString('ru-RU', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
}));
}
return dates;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question