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();
let week = new Date(timeStamp).getDay();
let weeks = ["日", "一", "二", "三", "四", "五", "六"];
let getWeek = "星期" + weeks[week];
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;
},
原理就是每次更新时间后,给个1000毫秒的定时器,也就是1秒,然后清除定时器再去创建,以此反复就形成了实时获取时间的效果