【相关文章推荐:vue.js】
vue.js显示时间的方法:
1.在data中定义一个变量,存储时间
data(){ return { nowtime:'' } },
2.给定一个div
<div>{{nowtime}}</div>
3.js部分
//显示当前时间(年月日时分秒) timeformate(timestamp) { let year = new date(timestamp).getfullyear(); let month =new date(timestamp).getmonth() + 1 < 10? "0" + (new date(timestamp).getmonth() + 1): new date(timestamp).getmonth() + 1; let date =new date(timestamp).getdate() < 10? "0" + new date(timestamp).getdate(): new date(timestamp).getdate(); let hh =new date(timestamp).gethours() < 10? "0" + new date(timestamp).gethours(): new date(timestamp).gethours(); let mm =new date(timestamp).getminutes() < 10? "0" + new date(timestamp).getminutes(): new date(timestamp).getminutes(); let ss =new date(timestamp).getseconds() < 10? "0" + new date(timestamp).getseconds(): new date(timestamp).getseconds(); this.nowtime = year + "年" + month + "月" + date +"日"+" "+hh+":"+mm+':'+ss ; }, nowtimes(){ this.timeformate(new date()); setinterval(this.nowtimes,1000); this.clear() }, clear(){ clearinterval(this.nowtimes) this.nowtimes = null; }
4.复制粘贴即可用,主要是使用定时器,每秒调用,最后清除定时器,清除函数
相关免费学习推荐:javascript(视频)
以上就是vue.js怎么样显示时间的详细内容。
