function begin() {
let nowDate = new Date()
let nowYear = nowDate.getFullYear()
// 上一年 = 今年-1年
let previousYear = nowYear - 1
// 判断最后一周的结束日期是多少
let strDay = previousYear + '-12-31'
let lastYearMoDay = new Date(strDay).getDay()
let objResArr = {
beginWeekDay: '',
endWeekDay: ''
}
let onjResStr=''
if (lastYearMoDay === 0) {
objResArr.beginWeekDay = previousYear + '/12/31'
objResArr.endWeekDay = previousYear + '/12/25'
} else {
let aa = (31 - lastYearMoDay + 1) > 10? (31 - lastYearMoDay + 1) : '0'+ (31 - lastYearMoDay + 1)
let bb = (7 - lastYearMoDay) > 10? (7 - lastYearMoDay) : '0'+ (7 - lastYearMoDay)
objResArr.beginWeekDay = previousYear + '/12/' + aa
objResArr.endWeekDay = nowYear + '/01/' + bb
}
onjResStr = objResArr.beginWeekDay + '-' + objResArr.endWeekDay + '~'
let obj = {
arr: objResArr,
str: onjResStr
}
let resArr = this.getDate(6,obj).resArr
let resSrt = this.getDate(6,obj).resStr
console.log(resArr)
console.log(resSrt)
}
// 根据最后一周获取前几周
function getDate(number, obj) {
let dataStr = new Date(obj.arr.endWeekDay)
let retrunArr = []
let retrunStr = []
retrunArr.push(obj.arr)
retrunStr.push(obj.str)
for (let i = 0; i < number; i ++) {
let firstDate = new Date(retrunArr[i].beginWeekDay)
let endDate = new Date(retrunArr[i].endWeekDay)
// 周一
let beginWeekDay = firstDate.getTime() - (7 * 24 * 3600 * 1000)
// 周日
let endWeekDay = endDate.getTime() - (7 * 24 * 3600 * 1000)
let obj2 = {
beginWeekDay: this.dateFormer(beginWeekDay),
endWeekDay: this.dateFormer(endWeekDay)
}
let str = obj2.beginWeekDay + '-' + obj2.endWeekDay + '~'
retrunStr.unshift(str)
retrunArr.push(obj2)
}
let resObj = {
resArr: retrunArr,
resStr: retrunStr
}
return resObj
}
// 日期格式化处理
function dateFormer (timeStamp) {
let time = new Date(timeStamp)
let y = time.getFullYear()
let m = time.getMonth() + 1
let d = time.getDate()
if (m < 10 ) {
m = '0'+ m;
}
if (d < 10) {
d = '0' + d;
}
return y + '/'+m+'/'+d
}
this.begin()
获取当前月的的本周,往前自动计算前6周
最新推荐文章于 2024-02-25 11:29:48 发布