需要生成数周的数组,前后空白。
[ //month
['','','',1,2,3,4] //week
...
[25,26,27,28,29,30,'']
]
答案 0 :(得分:0)
这就是我做的事情
const generateMonth = (date) => {
const getLocalDay = (date) => {
let day = date.getDay();
if (day == 0) {
day = 7;
}
return day;
};
const currentMonth = date.getMonth();
const firstDate = new Date(date.getFullYear(), currentMonth, 1);
const lastDate = new Date(date.getFullYear(), currentMonth + 1, 0);
let month = [];
let week = [];
for (let i = 1; i < getLocalDay(firstDate); i++) {
week.push('');
}
for (let i = 1; i <= lastDate.getDate(); i++) {
const tmpDate = new Date(date.getFullYear(), currentMonth, i);
if (getLocalDay(tmpDate) == 1) {
if (week.length) month.push(week);
week = [];
}
week.push(i);
}
for (let i = getLocalDay(lastDate); i < 7; i++) {
week.push('');
}
if (week.length) month.push(week);
return month;
};