获取过去几天时间,不包含当天的时间

73 阅读1分钟

6155418.png data里定义

 c_top: '1', // 时间筛选

获取时间

 search(type) {
      this.c_top = type || '1'
      if (this.c_top === '0') {
        //过去24小时不包含当天的 const arr = this.getTime(0, new Date(new Date().getTime() - 24 * 60 * 60 * 1000))
        //现在的时间往前推24小时
        const new24Time = parseTime((new Date().getTime() - 24 * 60 * 60 * 1000))
        const newTime = parseTime((new Date().getTime()))
        this.searchObj.startTime = new24Time
        this.searchObj.endTime = newTime
      } else if (this.c_top === '1') {
        const arr = this.getTime(7, new Date(new Date().getTime() - 24 * 60 * 60 * 1000))
        this.searchObj.startTime = arr[0]
        this.searchObj.endTime = arr[1]
        this.searchObj.startTime += ' 00:00:00'
        this.searchObj.endTime += ' 23:59:59'
      } else if (this.c_top === '2') {
        const arr = this.getTime(30, new Date(new Date().getTime() - 24 * 60 * 60 * 1000))
        this.searchObj.startTime = arr[0]
        this.searchObj.endTime = arr[1]
        this.searchObj.startTime += ' 00:00:00'
        this.searchObj.endTime += ' 23:59:59'
      } else if (this.c_top === '3') {
        if (this.time && this.time.length) {
          this.searchObj.startTime = this.time[0]
          this.searchObj.endTime = this.time[1]
          this.searchObj.startTime += ' 00:00:00'
          this.searchObj.endTime += ' 23:59:59'
        } else {
          this.searchObj.startTime = ''
          this.searchObj.endTime = ''
        }
      }


      // 进入页面就掉接口
      this.getstackeList()
      this.getCarList()
      this.pieChartList()
      this.getRcList()
      this.getLbList()
      this.getRbList()
    },
    /**
     * @description:获取时间
     * @param {String}
     */
    getTime(day, e, e1) {
      let t = new Date()
      if (e) {
        t = e
      }
      const y = t.getFullYear()
      const m = t.getMonth() + 1
      const d = t.getDate()
      const end = y + '-' + (m > 9 ? m : '0' + m) + '-' + (d > 9 ? d : '0' + d)
      let t1
      if (e1) {
        t1 = e1
      } else {
        t1 = new Date(t.getTime() - (day - 1) * 24 * 60 * 60 * 1000)
      }
      const y1 = t1.getFullYear()
      const m1 = t1.getMonth() + 1
      const d1 = t1.getDate()
      const start = y1 + '-' + (m1 > 9 ? m1 : '0' + m1) + '-' + (d1 > 9 ? d1 : '0' + d1)
      if (start > end) {
        return [end, start]
      } else {
        return [start, end]
      }
    },