在 methods 中进行时间方法,然后在 mounted()里面进行设置每秒调用一次。
<template>
<div>{{Samplingtime}}</div>
</template>
export default {
data() {
return {
//取样时间
Samplingtime:'',
};
},
mounted() {
this.getCurrentTime();
clearInterval(myTimeDisplay );
let myTimeDisplay = setInterval(() => {
this.getCurrentTime(); //每秒更新一次时间
}, 1000);
},
methods: {
//时间
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.Samplingtime=_this.gettime;
console.log(_this.gettime)
}
}
}