Vue获取当下日期时间及周几

Vue获取当下日期时间及周几
<span>{{nowWeek}}</span>    
<span>{{nowDate}}</span>    
<span>{{nowTime}}</span>

 

script

data () {
  return {
    timer: null,
    nowWeek: '',
    nowDate: '',
    nowTime: ''
  }
}

 mounted

mounted () {
  this.timer = setInterval(() => {
    this.setNowTimes()
  }, 1000)
}

   methods

setNowTimes () {
  let myDate = new Date()
  // console.log(myDate)
  let wk = myDate.getDay()
  let yy = String(myDate.getFullYear())
  let mm = myDate.getMonth() + 1
  let dd = String(myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate())
  let hou = String(myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours())
  let min = String(myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes())
  let sec = String(myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds())
  let weeks = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  let week = weeks[wk]
  this.nowDate = yy + '-' + mm + '-' + dd
  this.nowTime = hou + ':' + min + ':' + sec
  this.nowWeek = week
}

  

上一篇:js将时间戳转换为年月日时分秒


下一篇:将 时间戳 转换为 具体时间