美文网首页
小知识积累 -- 获取当前日期的前一周/几天

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

作者: 疯泽清 | 来源:发表于2021-01-07 14:15 被阅读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

    },

相关文章

网友评论

      本文标题:小知识积累 -- 获取当前日期的前一周/几天

      本文链接:https://www.haomeiwen.com/subject/qjjzoktx.html