![]()
![]()
![]()
this.timer = setInterval(this.getTime, 1000);
![]()
//实时刷新时间
getTime() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
this.month = check(month);
this.day = check(day);
this.hour = check(hour);
this.minute = check(minute);
this.second = check(second);
function check(i) {
const num = (i < 10) ? ("0" + i) : i;
return num;
}
this.nowDate = year + "年" + this.month + "月" + this.day + "日";
this.nowTime = this.hour + ":" + this.minute + ":" + this.second;
this.time = ' 星期' + '日一二三四五六'.charAt(new Date().getDay())
},
beforeDestroy() {
if (this.timer) {
clearInterval(this.timer); // 在Vue实例销毁前,清除定时器
}
},