0
点赞
收藏
分享

微信扫一扫

获取今天的日期(年月日时分秒)、获取今天昨天明天的日期

眸晓 2022-03-11 阅读 59

1、获取今天的日期(年月日时分秒)

 getCurrentTime() {
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth()+1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
      this.gettime = yy+'/'+mm+'/'+dd+' '+hh+':'+mf+':'+ss;
      console.log(this.gettime)  
    }

2、获取昨天今天明天

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)
 },

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
    }

举报

相关推荐

0 条评论