时间格式化:年 - 月 - 日

时间格式化:年 - 月 - 日
时间格式化:年 - 月 - 日
mounted() {
// 时间定时器
let _this = this // 声明一个变量指向Vue实例this,保证作用域一致
this.timer = setInterval(() => {
let a = new Date()
let nian = a.getFullYear()
let yue = (a.getMonth() + 1).toString().padStart(2, ‘0’)
let ri = a.getDate().toString().padStart(2, ‘0’)
// let shi = a.getHours().toString().padStart(2, ‘0’)
// let fen = a.getMinutes().toString().padStart(2, ‘0’)
_this.date = nian + “-” + yue + “-” + ri // 修改数据date
}, 0)
},

最后记得清除定时器时间格式化:年 - 月 - 日
beforeDestroy() {
if (this.timer) {
clearInterval(this.timer); // 在Vue实例销毁前,清除定时器
}
}