html
<span class="fd-login-name" v-text="loginName"></span>
js
mounted() {
this.getTime();
const timer = 1000;
this.timer = setInterval(() => {
this.getHMS();
}, timer);
},
methods: {
// 获取年月日
getTime() {
const time = new Date();
// 获取当年 2022
const year = time.getFullYear();
// 返回0 到 11:0 代表一月份,1 代表二月份, 2 代表三月份,依次类推。
const month = this.zeroFlag(time.getMonth() + 1);
// 返回一个1 到 31的整数值。
const day = time.getDate();
this.time = `${year}-${month}-${day}`;
this.getHMS();
},
// 获取时分秒
getHMS() {
const time = new Date();
// 返回一个0 到 23之间的整数值
const hours = this.zeroFlag(time.getHours());
// 返回一个0 到 59的整数值。
const miunte = this.zeroFlag(time.getMinutes());
// 该方法返回一个 0 到 59 的整数值。
const second = this.zeroFlag(time.getSeconds());
const timer = `${hours}:${miunte}:${second}`;
const timeFirst = this.time.split(' ');
this.time = `${timeFirst[0]} ${timer}`;
},
// 小于9在前面加0
zeroFlag(num) {
const NUM = 9;
return num>NUM ? num : `0${num}`;
}
},
beforeDestroy() {
clearInterval(this.timer);
}
效果
2023-10-18 11:51:27