前端实现自定义时间实时显示,并根据北京时间实时同步显示每秒跳动
onLoad() {
this.updateBeijingTime();
},
updateBeijingTime: function () {
let that = this
setInterval(function () {
let beijingTime = that.getBeijingTime()
that.setData({
beijingTime: beijingTime
})
}, 1000)
},
getBeijingTime: function () {
let now = new Date();
let utc = now.getTime() + (now.getTimezoneOffset() * 60000)
let beijingTime = new Date(utc + (3600000 * 8))
let hours = beijingTime.getHours()
let minutes = beijingTime.getMinutes();
let seconds = beijingTime.getSeconds();
return this.formatTime(hours) + ':' + this.formatTime(minutes) + ':' + this.formatTime(seconds)
},
formatTime: function (time) {
return time < 10 ? '0' + time : time
},