getStartEndDate (dayLength) { // 计算开始时间及结束时间,参数为日期长度
let arr = []
const startDate = '2018-02-1'
const startYear = Number(startDate.slice(0, 4))
const startMonth = Number(startDate.slice(5, 7))
const startDay = Number(startDate.slice(8, 10))
// const startDay = 2
console.log('开始:', startYear, startMonth, startDay)
// 判断起始月份有多少天
let thisMonthDay = new Date(startYear, startMonth, 0).getDate()
// console.log(thisMonthDay)
// 判断本天离月底还剩多少天
let thisRemainDay = new Date(startYear, startMonth, 0).getDate() - startDay
// 判断下月还需锻炼多少天
let nextMonthDay = 31 - thisRemainDay
// 生成一个数组
// 判断开始天是否 > 3,是则数组从本月开始天数 - 3 开始,否则获取上一月份最后3天日期
if (startDay > 3) {
// 再做一层判定:当起始月份为1月且日期 > 28,显示1、2、3三个月
console.log(startDay - 3, thisMonthDay, nextMonthDay)
if (startMonth === 1 && startDay > 28) {
let once = true
for (let i = startDay - 3; i <= thisMonthDay + nextMonthDay; i++) {
let j = i % thisMonthDay
if (i % thisMonthDay === 0) {
j = thisMonthDay
once = !once
if (!once) {
thisMonthDay = new Date(startYear, 2, 0).getDate() // %2月天数
i = i - 3
}
}
arr.push(j)
}
} else {
for (let i = startDay - 3; i <= thisMonthDay + nextMonthDay; i++) {
let j = i % thisMonthDay
if (i % thisMonthDay === 0) {
j = thisMonthDay
}
arr.push(j)
}
}
} else {
// 获取上月日期
const beforeMonthDate = new Date(startYear, startMonth - 1, 0).getDate()
console.log(beforeMonthDate, beforeMonthDate - (4 - startDay))
for (let i = beforeMonthDate; i > beforeMonthDate - (4 - startDay); i--) {
arr.unshift(i)
}
for (let j = 1; j <= dayLength - (4 - startDay); j++) {
let i = j % thisMonthDay
if (j % thisMonthDay === 0) {
i = thisMonthDay
}
arr.push(i)
}
}
console.log(arr)
}
console