Vue 获取今天日期 date,时间,周几,weak

js 获取当月第一天和最后一天
2019-12-13 17:19:01
var year=new Date().getFullYear() ;
var month=new Date().getMonth()+1 ;
var day = new Date(year,month,0); 
starResult.innerText =year + "-" + month + "-01";
endResult.innerText = year + '-' + month + '-' + day.getDate(); 
在这里插入代码片

星期五 2021-7-09 16:45:03

{{nowWeek}}    
{{nowDate}}    
{{nowTime}}

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]
  let apm = 'AM'
  if (myDate.getHours() > 11) {
    apm = 'PM'
  }
  this.nowDate = yy + '-' + mm + '-' + dd
  this.nowTime = hou + ':' + min + ':' + sec
  this.nowWeek = week
}

from : https://www.cnblogs.com/lvsige/p/13474932.html

你可能感兴趣的:(js,vue,vue.js,javascript,前端)