获取当前月的的本周,往前自动计算前6周

在这里插入图片描述

 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值