在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>
就是他妈的这么简单
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。