js-中国标准时间转化为年月日时分秒
created() {
let myTime = "Wed Feb 16 2022 00:00:00 GMT+0800";
let newTime = this.filterTime( myTime)
console.log(newTime)
},
methods: {
filterTime(time) {
let date = new Date(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 = new Date();
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);
},
fn(str) {
str < 10 ? str = "0" + str : str;
return str;
},
mounted(){
setInterval(() => {
this.getTime()
}, 1000)
}