const getDaysInMonth = (month, year) => { // 得到不同月份的天数
switch (month) {
default:
break;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return (((((year % 4) === 0) && ((year % 100) !== 0)) || ((year % 400) === 0)) ? 29 : 28);
}
return 31;
};
const getCalendar = (month, year) => { // 得到每月日历里的日期数组
const days = getDaysInMonth(month + 1, year);
const firstDays = new Date(year, month, 1);
const startPos = firstDays.getDay();
const daylist = new Array(startPos).fill(null)
.concat(new Array(days).fill(null).map((__, i) => new Date(year, month, i + 1)));
const c = [];
while (daylist.length) {
c.push(daylist.splice(0, 7));
}
return c;
};
时间: 2024-10-05 07:38:54