在wxml中设置显示时间的标签
<text>{{nowDate}}</text>
<text>{{nowTime}}</text>
在js中data中设置
data: {
nowDate: '',
nowTime: '',
},
在js中继续设置
onLoad(options) {
this.currentTime()
},
currentTime() {
setInterval(this.formatDate, 500);
},
formatDate() {
//获取当前时间并打印
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
this.setData({
nowDate: this.nowDate,
nowTime: this.nowTime,
})
},
这样即可在小程序中显示实时时间了