<template>
<div>{{nowTime}}</div>
</template>
<script>
export default {
data() {
return {
nowTime : "", // 当前日期
};
},
methods: {
currentTime() {
setInterval(this.getCurrentTime, 500);
},
getCurrentTime() {
//获取当前时间并打印
var _this = this;
let yy = new Date().getFullYear();
let mm = new Date().getMonth() + 1;
let dd = new Date().getDate();
let hh = new Date().getHours();
let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
_this.gettime = yy + '年' + mm + '月' + dd + '日 ' + hh + ':' + mf + ':' + ss;
this.nowTime = _this.gettime
}
},
mounted() {
this.currentTime();
},
// 销毁定时器
beforeDestroy() {
if (this.getCurrentTime) {
clearInterval(this.getCurrentTime); // 在Vue实例销毁前,清除时间定时器
}
}
};
</script>
结果样式: