created(){// 中国标准时间let myTime ="Wed Feb 16 2022 00:00:00 GMT+0800";let newTime =this.filterTime( myTime)
console.log(newTime)// 2022-02-16 00:00:00},methods:{filterTime(time){let date =newDate(time);let y = date.getFullYear();let m = date.getMonth()+1;
m = m <10?"0"+ m : m;let d = date.getDate();
d = d <10?"0"+ d : d;let h = date.getHours();
h = h <10?"0"+ h : h;let minute = date.getMinutes();
minute = minute <10?"0"+ minute : minute;let s = date.getSeconds();
s = s <10?"0"+ s : s;return y +"-"+ m +"-"+ d +" "+ h +":"+ minute +":"+ s;},}
js- 利用 setInterval函数实现当前时间
//获取当前时间getTime(){var date =newDate();var year = date.getFullYear();var mth = date.getMonth()+1;var d = date.getDate();var h = date.getHours();var m = date.getMinutes();var s = date.getSeconds();var week = date.getDay();//定义拼接变量,变量调用转换两位数函数this.timeNow = year +"年"+this.fn(mth)+"月"+this.fn(d)+"日"+this.fn(h)+":"+this.fn(m)+":"+this.fn(s);},//定义函数,将一位数"1"转为两位数"01"fn(str){
str <10? str ="0"+ str : str;return str;},//最后在vue的mounted生命周期里面使用setInterval函数mounted(){setInterval(()=>{this.getTime()},1000)}