关于前端:Vue-获取当下日期时间及周几

41次阅读

共计 800 个字符,预计需要花费 2 分钟才能阅读完成。

html:
<div>

<p>{{nowWeek}}</p>
<p><span>{{nowDate}}</span>{{nowTime}}</p>

</div>

script:

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

}

mounted:

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

methods:

setNowTimes () {  
    // 获取以后工夫
    let myDate = new Date()  
    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

}`

正文完
 0