Date.prototype.addDays = function(days) { // https://stackoverflow.com/questions/563406/how-to-add-days-to-date
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
function date4y2m2d(date) {
const _format = n => {
return n < 10 ? '0' + n : n;
};
return `${ date.getFullYear() }-${ _format(date.getMonth() + 1) }-${ _format(date.getDate()) }`;
}
let date = new Date();
let days = [date4y2m2d(date)];
for (let i = 1; i < 5; i++) {
days.push( date4y2m2d(date.addDays(i)) );
}
console.log(days); //готово
[0, 1, 2, 3, 4].map(
(shift) => {
const date = new Date();
date.setDate(date.getDate() + shift);
return date.toLocaleDateString('en-CA');
}
);
// Array(5) [ "2022-03-28", "2022-03-29", "2022-03-30", "2022-03-31", "2022-04-01" ]
const date = new Date();
const dates = [];
for (i = 0; i < 5; i += 1) {
dates.push(date.toLocaleDateString('en-CA'));
date.setDate(date.getDate() + 1);
}
console.log(dates);
// Array(5) [ "2022-03-28", "2022-03-29", "2022-03-30", "2022-03-31", "2022-04-01" ]