js时间戳格式和2022-01-27 00:00:00格式相互转换
时间戳转2022-01-27 00:00:00(年月日时分秒)格式
this.formDate(new Date(1643126400), "yyyy-MM-DD HH:mm:ss")
2022-01-27 00:00:00(年月日时分秒)格式转时间戳
let curDate = Date.parse(new Date('2022-01-27 00:00:00'))
或者
let curDate =new Date('2022-01-27 00:00:00').getTime()
格式化工具函数
1.如何调用函数:
this.formDate(this.datetime, "yyyy-MM-DD HH:mm:ss")
2.代码如下(示例):
formDate(data, format) {
// console.log("data,time");
let time = {
"M+": data.getMonth() + 1,
"D+": data.getDate(),
"H+": data.getHours(),
"m+": data.getMinutes(),
"s+": data.getSeconds(),
};
if (/(y+)/i.test(format)) {
format = format.replace(
RegExp.$1,
(data.getFullYear() + "").substr(4 - RegExp.$1.length)
);
}
for (let k in time) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1
? time[k]
: ("00" + time[k]).substr(("" + time[k]).length)
);
}
}
return format;
}