一、在方法中写入以下代码:

methods: {
   getDay (day, hours) {
      var today = new Date()
      var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day + hours
      today.setTime(targetday)
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      var getHours = today.getHours()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate + '小时:' + getHours
    },
    doHandleMonth (month) {
      var m = month
      if (month.toString().length === 1) {
        m = '0' + month
      }
      return m
    },
   }

二、测试:

mounted () {
    console.log('昨天:', this.getDay(-1, 7200000))
    console.log('今天:', this.getDay(0, 3600000))
    console.log('明天:', this.getDay(1, 3600000))
    console.log('一周后:', this.getDay(7, 7200000)
 }

效果:

在这里插入图片描述

Logo

前往低代码交流专区

更多推荐