根据百度的方法封装的js函数
function getDateFormat(date,format) {
let time = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
'S+': date.getMilliseconds(), //毫秒
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1,(date.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;
}
console.log(getDateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss'));
输出结果