不少時候,爲了顯示完整,須要顯示上下月的殘餘數據。通常來講,日曆展現時,最大是7 X 6 = 42位,爲啥是42位,呃,本身去想一想吧。當月天數已知,上月殘餘天數,咱們能夠用當月1號是周幾來推斷出來,下月殘餘天數,正好用42 - 當月天數 -上月殘餘。
// 上月 年、月
preMonth(year, month) {
if (month == 1) {
return {
year: --year,
month: 12
}
} else {
return {
year: year,
month: --month
}
}
},
// 獲取當月中,上月多餘數據,返回數組
getPreArr(){
let preMonthDateLen = this.getFirstDateWeek(this.data.currentYear, this.data.currentMonth) // 當月1號是周幾 == 上月殘餘天數)
let preMonthDateArr = [] // 定義空數組
if (preMonthDateLen > 0) {
let { year, month } = this.preMonth(this.data.currentYear, this.data.currentMonth) // 獲取上月 年、月
let date = this.getDateLen(year, month) // 獲取上月天數
for (let i = 0; i < preMonthDateLen; i++) {
preMonthDateArr.unshift({ // 尾部追加
month: 'pre', // 只是爲了增長標識,區分當、下月
date: date
})
date--
}
}
this.setData({
preMonthDateLen
})
return preMonthDateArr
},
// 下月 年、月
nextMonth(year, month) {
if (month == 12) {
return {
year: ++year,
month: 1
}
} else {
return {
year: year,
month: ++month
}
}
},
// 獲取當月中,下月多餘數據,返回數組
getNextArr() {
let nextMonthDateLen = 42 - this.data.preMonthDateLen - this.data.currentMonthDateLen // 下月多餘天數
let nextMonthDateArr = [] // 定義空數組
if (nextMonthDateLen > 0) {
for (let i = 1; i <= nextMonthDateLen; i++) {
nextMonthDateArr.push({
month: 'next',// 只是爲了增長標識,區分當、上月
date: i
})
}
}
return nextMonthDateArr
},
整合三組數據,就獲得了完整的當月數據,格式以下
[
{month: "pre", date: 30},
{month: "pre", date: 31},
{month: "current", date: 1},
{month: "current", date: 2},
...
{month: "current", date: 31},
{month: "next", date: 1},
{month: "next", date: 2}
]