直接代碼
function getMonthDays(year, month) {
let firstDay = new Date(year, month, 1)
let lastDay = new Date(year, month + 1, 0)
var rawDays = []
for (var i = 1; ; i++) {
var tempDay = new Date(year, month, i)
if (tempDay.getTime() < lastDay.getTime()) {
rawDays.push(tempDay)
} else {
break
}
}
var tempDay = new Date(firstDay.getFullYear(), firstDay.getMonth(), firstDay.getDate() - 1)
while (tempDay.getDay() != 0) {
rawDays.unshift(tempDay)
tempDay = new Date(tempDay.getFullYear(), tempDay.getMonth(), tempDay.getDate() - 1)
}
rawDays.unshift(tempDay)
tempDay = new Date(lastDay.getFullYear(), lastDay.getMonth(), lastDay.getDate())
while (tempDay.getDay() != 6) {
rawDays.push(tempDay)
tempDay = new Date(tempDay.getFullYear(), tempDay.getMonth(), tempDay.getDate() + 1)
}
rawDays.push(tempDay)
var acturalDays = []
for (var i = 0; i < rawDays.length;) {
var t = 0;
var tempWeek = []
while (t < 7 && rawDays.length > i) {
if (rawDays.length > i) {
tempWeek.push(rawDays[i++])
}
t++
}
acturalDays.push(tempWeek)
}
var result = []
for (var week of acturalDays) {
var temp = []
for (var day of week) {
temp.push({
date: day,
type: day.getTime() < firstDay.getTime() ? "pre" : day.getTime() > lastDay.getTime() ? "suf" : "cur",
stamp: day.getTime() / 1000
})
}
result.push(temp)
}
return result
}