/* 获取当前时间 */
getCurrentTime() {
//获取当前时间并打印
const _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 = `${this.formatTime(yy)}/${this.formatTime(
mm
)}/${this.formatTime(dd)} ${this.formatTime(hh)}:${mf}:${ss}`;
console.log(_this.gettime);
},
/* 将单个数字转换为两位数,例如:1 -> 01 */
formatTime(value) {
return value < 10 ? `0${value}` : value;
},
然后在 mounted 中调用该方法
mounted() {
// 在组件挂载后获取当前时间
this.getCurrentTime();
},
实现效果: