<template>
<div>
<div class="time">{{time}}</div>
</div>
</template>
<script>
export default {
name: "timer",
data() {
return {
//显示时间
time:'',
date:'',
}
},
mounted() {
this.dateShow();
},
created() {
this.dateFormat();
},
beforeDestroy() {
this.dataDestroy();
},
methods: {
dataDestroy(){
if (this.timer) {
clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器
}
},
dateShow() {
this.timer = setInterval(() => {
this.dateFormat()
}, 1000)
},
dateFormat() {
this.date = new Date();
let year = this.date.getFullYear();
let month = this.date.getMonth() + 1 < 10 ? "0" + (this.date.getMonth() + 1) : this.date.getMonth() + 1;
let day = this.date.getDate() < 10 ? "0" + this.date.getDate() : this.date.getDate();
let hours = this.date.getHours() < 10 ? "0" + this.date.getHours() : this.date.getHours();
let minutes = this.date.getMinutes() < 10 ? "0" + this.date.getMinutes() : this.date.getMinutes();
let seconds = this.date.getSeconds() < 10 ? "0" + this.date.getSeconds() : this.date.getSeconds();
this.time = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
},
}
}
</script>
02-22
1706
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-13
6608
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-02
565
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-26
186
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交