小知识积累 -- 获取当前日期的前一周/几天

2021-01-07  本文已影响0人  疯泽清

功能:element-ui 的 el-date-picker 请求发送 默认当前时间区间  (前两天-至今)

调用:

 this.dialogParams.StartDate = this.getTime(-2)  //前两天

  this.dialogParams.EndDate = this.getTime(0)  //当天

方法如下:

getTime(day) {

      const today = new Date()

      const targetday = today.getTime() + 1000 * 60 * 60 * 24 * day

      today.setTime(targetday) //关键代码

      const tYear = today.getFullYear()

      let tMonth = today.getMonth()

      let tDate = today.getDate()

      tMonth = this.doHandleMonth(tMonth + 1)

      tDate = this.doHandleMonth(tDate)

      return tYear + '-' + tMonth + '-' + tDate

    },

    doHandleMonth(month) {

      let m = month

      if (month.toString().length === 1) {

        m = '0' + month

      }

      return m

    },

上一篇下一篇

猜你喜欢

热点阅读