在data中定义一个字符串用来接收格式化之后的时间:
data() {
return {
todayDate: "",
};
},
在方法中写一个nowTime,并将新值赋值给todayDate:(注意里面有个自定义的check函数)
nowTime() {
let myDate = new Date();
let year = myDate.getFullYear();
let month = myDate.getMonth() + 1;
let date = myDate.getDate();
let hours = myDate.getHours();
let minutes = myDate.getMinutes();
let seconds = myDate.getSeconds();
var week = "星期" + "日一二三四五六".charAt(myDate.getDay());
hours = this.check(hours);
minutes = this.check(minutes);
seconds = this.check(seconds);
this.todayDate =
hours + ":" + minutes+' '+ week +' '+ month+'/'+date+'/'+year;
},
// 检验时间补零的方法
check(i) {
let num;
i < 10 ? (num = "0" + i) : (num = i);
return num;
},
在某个初始化方法中藏入一个定时器:
setInterval(this.nowTime, 1000);
在页面中即可直接使用todayDate
<div class="date">{{todayDate}}</div>
就是他妈的这么简单