function format(date, ft = "yyyy-MM-dd hh:mm:ss") {
if (!date) return;
if (Object.prototype.toString.apply(date) === "[object String]") {
date = new Date(date);
}
let o = {
"(M+)": `${date.getMonth() + 1}`, // 月
"((D|d)+)": `${date.getDate()}`, // 日
"((H|h)+)": `${date.getHours()}`, // 小时
"(m+)": `${date.getMinutes()}`, // 分
"(s+)": `${date.getSeconds()}` // 秒
};
// 年份是4位,做特殊处理
if (/(([Yy])+)/.test(ft)) {
ft = ft.replace(
RegExp.$1,
`${date.getFullYear()}`.substr(4 - RegExp.$1.length)
);
}
Object.keys(o).forEach(key => {
if (new RegExp(key).test(ft)) {
let value = o[key];
ft = ft.replace(
RegExp.$1,
RegExp.$1.length === 1 ? value : `00${value}`.substr(value.length)
);
}
});
return ft;
}