废话不多说先上代码。。。
首先是构建当前时间到目标时间月份
//startYear 为目标年份
// startMont 为目标月
initMonth(){
const startYear = 2022
const startMont = 6
const now = new Date()
const nowYear = now.getFullYear()
const nowMonth = now.getMonth() + 1// 当前月
// 相差月份
let initMonthNum = 0
if (nowYear - startYear === 0) {
initMonthNum = nowMonth - startMont
} else {
initMonthNum = (nowYear - startYear) * 12 - 6 + nowMonth
}
const monthArr = []
for (let i = 0, len = initMonthNum; i < len; i++) {
const month = nowMonth - i
// 本年倒数月份时 正常构建
if (i < nowMonth) {
const initData = new Date(nowYear, month, 0)
const endDay = initData.getDate()
monthArr.push({
startTime: `${nowYear}/${month}/01`,
endTime: `${nowYear}/${month}/${endDay}`,
initMonth: month,
initYear: nowYear,
name: `${nowYear}年${month}月`
})
} else {
// 循环年数
const loopYear = Math.floor(month / 12)
// 构建月份
const initMonth = month - 12 * loopYear === 0 ? 12 : month - 12 * loopYear
// 相差年份
let differYear = 1
if ((i + 12 - nowMonth) > 12) differYear = Math.floor((i + 12 - nowMonth) / 12)
const initData = new Date(nowYear - differYear, initMonth, 0)
const endDay = initData.getDate()
monthArr.push({
startTime: `${nowYear - differYear}/${initMonth}/01`,
endTime: `${nowYear - differYear}/${initMonth}/${endDay}`,
initMonth: initMonth,
initYear: nowYear,
name: `${nowYear}年${initMonth}月`
})
}
}
return monthArr
},
其次是根据传入年,月 构建传入日期的每周,每半月
// 构建每周
initWeeks(year, month) {
const weekDay = []
const now = new Date()
const nowMonth = now.getMonth() + 1// 当前月
const nowYear = now.getFullYear()
const nowDay = now.getDate()
const endDay = now.getDate()
// 该月第一天
now.setFullYear(year, month - 1, 1)
var w1 = now.getDay()
if (w1 === 0) {
w1 = 7
}
// 该月天数
now.setFullYear(year, month, 0)
var dd = now.getDate()
// 第一个周一
let wDay1
if (w1 !== 1) {
wDay1 = 7 - w1 + 2
} else {
wDay1 = 1
}
// 当前月有几周
const week_count = Math.ceil((dd - wDay1 + 1) / 7)
for (let i = 0, len = week_count; i < len; i++) {
const num = (i + 1) * 7
const end = num + wDay1 - 1
const start = end - 6
if (month === nowMonth && year === nowYear && nowDay < end) {
} else {
weekDay.push({
startTime: `${year}/${month}/${start}`,
endTime: `${year}/${end > endDay ? month + 1 : month}/${end > endDay ? end - endDay : end}`,
name: `${year}年${month}月第${this.toZnNum(i+1)}周`,
})
}
}
return weekDay
},
// 构建每半月
initMonths(year, month) {
const now = new Date()
const nowMonth = now.getMonth() + 1// 当前月
const nowYear = now.getFullYear() // 当前年份
const nowDay = now.getDate() // 当日
const initDay = new Date(year, month, 0)
const endDay = initDay.getDate() // 传入月份有几天
// 该月第一天
initDay.setFullYear(year, month - 1, 1)
let monthArr = []
// 当前月份 大于16号时才有上半月
if(month === nowMonth && year === nowYear && nowDay < 16){
}else if(month === nowMonth && year === nowYear && nowDay > 15){
monthArr.push(
{
startTime: `${year}/${month}/01`,
endTime: `${year}/${month}/15`,
name: `${year}年${month}月上半月`
}
)
}else{
monthArr.push(
{
startTime: `${year}/${month}/01`,
endTime: `${year}/${month}/15`,
name: `${year}年${month}月上半月`
},
{
startTime: `${year}/${month}/16`,
endTime: `${year}/${month}/${endDay}`,
name: `${year}年${month}月下半月`
}
)
}
return monthArr
},
根据业务需求每月的周数要求中文数字所以额外便携式toZnNum方法
// 数学数字转中文数字
toZnNum(num){
let znNum = ''
switch (num) {
case 1:
znNum = '一'
break;
case 2:
znNum = '二'
break;
case 3:
znNum = '三'
break;
case 4:
znNum = '四'
break;
case 5:
znNum = '五'
break;
default:
break;
}
return znNum
}
最后的运用
// 检查周期: 1-每月 2-每两周 3-每周
this.monthArr = this.initMonth()
if(this.checkTheFrequency == 1 ) {
this.monthArr.shift()
this.previousList = this.monthArr
}
if(this.checkTheFrequency == 2 ) {
this.monthArr.forEach( item =>{
let arr = this.initMonths(item.initYear,item.initMonth)
this.previousList = this.previousList.concat(arr)
})
}
if(this.checkTheFrequency == 3 ) {
this.monthArr.forEach( (item,index) =>{
let arr = this.initWeeks(item.initYear,item.initMonth)
if(index == 0) this.activeName = arr.length - 1
this.previousList = this.previousList.concat(arr)
})
}
// this.previousList 为最后接收数组
// this.checkTheFrequency 为检查频率 1-每月 2-每两周 3-每周