const today = new Date();
const getWeeks = (month, year) => {
const date = new Date(year, month + 1, 0, 0, 0, 0);
const weeks = [];
const days = date.getDate();
let startWeek = null;
for (let day = 1; day <= days; day++) {
date.setDate(day);
const weekDay = date.getDay();
if (weekDay > 0 && startWeek === null) {
startWeek = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
}
if (weekDay === 0 || day === days) {
weeks.push({
start: startWeek,
end: new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)
});
startWeek = null;
}
}
return weeks;
};
console.log(JSON.stringify(getWeeks(1, 2021), null, 4));
/*
У меня UTC+6, поэтому в датах 6 вечера.
[
{
"start": "2021-01-31T18:00:00.000Z",
"end": "2021-02-06T18:00:00.000Z"
},
{
"start": "2021-02-07T18:00:00.000Z",
"end": "2021-02-13T18:00:00.000Z"
},
{
"start": "2021-02-14T18:00:00.000Z",
"end": "2021-02-20T18:00:00.000Z"
},
{
"start": "2021-02-21T18:00:00.000Z",
"end": "2021-02-27T18:00:00.000Z"
}
]
*/