获取一个实时走动的时间
<div>
<h1>时间</h1>
<span>{{ nowData }}</span>
<span>{{ nowTime }}</span>
</div>
data() {
return {
nowTime: "",
nowData: "",
}
}
mounted() {
//时间转换:
this.timer = setInterval(() => {
let date = new Date();
let year = date.getFullYear();
//使用三元表达式在小于10的前面加0,以达到格式统一:
let month =
date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1;
let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
let hours =
date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
let minutes =
date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
let seconds =
date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
// 拼接
this.nowTime = hours + ":" + minutes + ":" + seconds;
this.nowData = year + "." + month + "." + day + " ";
}, 1000);
},