js获取今天、昨天、近三天、本周、上周、本月、上月、本季度、上季度、本年、上年、

js获取今天、昨天、近三天、本周、上周、本月、上月、本季度、上季度、本年、上年、

getDateTypeValue(type) {
      let d = new Date();
      let startDate = ''
      let endDate = ''
      // 今天
      if (type == 0) {
        startDate = this.getYYYYMMDD(d)
        endDate = startDate;
      } else if (type == 1) {
        // 昨天
        d.setDate(d.getDate() - 1);
        startDate = this.getYYYYMMDD(d)
        endDate = startDate;
      } else if (type == 2) {
        // 近三天
        endDate = this.getYYYYMMDD(d)
        d.setDate(d.getDate() - 2);
        startDate = this.getYYYYMMDD(d)
      } else if (type == 3) {
        // 本周
        const week = d.getDay()
        //一天的毫秒数
        const millisecond = 1000 * 60 * 60 * 24
        //减去的天数
        const minusDay = week != 0 ? week - 1 : 6
        //本周 周一
        const monday = new Date(d.getTime() - minusDay * millisecond)
        //本周 周日
        const sunday = new Date(monday.getTime() + 6 * millisecond)
        startDate = this.getYYYYMMDD(monday)
        endDate = this.getYYYYMMDD(sunday)
      } else if (type == 4) {
      // 上周
        let weekNum = d.getDay()
        weekNum = weekNum == 0 ? 7 : weekNum
        let lastDate = new Date(d.getTime() - weekNum * 24 * 60 * 60 * 1000)
        let firstDate = new Date(d.getTime() - (weekNum + 6) * 24 * 60 * 60 * 1000)
        startDate = this.getYYYYMMDD(firstDate)
        endDate = this.getYYYYMMDD(lastDate)
      } else if (type == 5) {
        // 近7天
        endDate = this.getYYYYMMDD(d)
        d.setDate(d.getDate() - 6);
        startDate = this.getYYYYMMDD(d)
      } else if (type == 6) {
        // 近14天
        endDate = this.getYYYYMMDD(d)
        d.setDate(d.getDate() - 13);
        startDate = this.getYYYYMMDD(d)
      } else if (type == 7) {
        // 本月
        let s1 = new Date(d.getFullYear(), d.getMonth()+1, 0)
        let s2 = new Date(d.getFullYear(), d.getMonth(), 1)
        endDate = this.getYYYYMMDD(s1)
        startDate = this.getYYYYMMDD(s2)
      } else if (type == 8) {
        // 上月
        let s1 = new Date(d.getFullYear(), d.getMonth(), 0)
        let s2 = new Date(d.getFullYear(), d.getMonth()-1, 1)
        endDate = this.getYYYYMMDD(s1)
        startDate = this.getYYYYMMDD(s2)
      } else if (type == 9) {
        // 本季
        let month = d.getMonth() + 1;//getMonth返回0-11
        let year = d.getFullYear();
        if(month >=1 && month <=3){
          startDate = this.getYYYYMMDD(new Date(year, 0, 1));
          endDate = this.getYYYYMMDD(new Date(year, 3, 0));
        }else if(month >=4 && month <=6){
          startDate = this.getYYYYMMDD(new Date(year, 3, 1));
          endDate = this.getYYYYMMDD(new Date(year, 6, 0));
        }else if(month >=7 && month <=9){
          startDate = this.getYYYYMMDD(new Date(year, 6, 1));
          endDate = this.getYYYYMMDD(new Date(year, 9, 0));
        }else{
          startDate = this.getYYYYMMDD(new Date(year, 9, 1));
          endDate = this.getYYYYMMDD(new Date(year, 12, 0));
        }
      } else if (type == 10) {
        // 上季
        let month = d.getMonth() + 1;//getMonth返回0-11
        let year = d.getFullYear();
        if(month >=1 && month <=3){
          startDate = this.getYYYYMMDD(new Date(year-1, 9, 1));
          endDate = this.getYYYYMMDD(new Date(year-1, 12, 0));
        }else if(month >=4 && month <=6){
          startDate = this.getYYYYMMDD(new Date(year, 0, 1));
          endDate = this.getYYYYMMDD(new Date(year, 3, 0));
        }else if(month >=7 && month <=9){
          startDate = this.getYYYYMMDD(new Date(year, 3, 1));
          endDate = this.getYYYYMMDD(new Date(year, 6, 0));
        }else{
          startDate = this.getYYYYMMDD(new Date(year, 6, 1));
          endDate = this.getYYYYMMDD(new Date(year, 9, 0));
        }
      } else if (type == 11) {
        // 本年
        let year = d.getFullYear();
        startDate = this.getYYYYMMDD(new Date(year, 0, 1));
        endDate = this.getYYYYMMDD(new Date(year, 12, 0));
      }  else if (type == 12) {
        // 上年
        let year = d.getFullYear();
        startDate = this.getYYYYMMDD(new Date(year-1, 0, 1));
        endDate = this.getYYYYMMDD(new Date(year-1, 12, 0));
      }
      this.queryForm.startDateStr = startDate
      this.queryForm.endDateStr = endDate
    },
getYYYYMMDD(d) {
      return d.getFullYear() + '-' + (d.getMonth() + 1).toString().padStart(2, '0') + '-' + d.getDate().toString().padStart(2, '0')
    },
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值