function formatDate(t,str){
const obj = {
yyyy:t.getFullYear(),
yy:(""+ t.getFullYear()).slice(-2),
M:t.getMonth()+1,
MM:("0"+ (t.getMonth()+1)).slice(-2),
d:t.getDate(),
dd:("0" + t.getDate()).slice(-2),
H:t.getHours(),
HH:("0" + t.getHours()).slice(-2),
h:t.getHours() % 12,
hh:("0"+t.getHours() % 12).slice(-2),
m:t.getMinutes(),
mm:("0" + t.getMinutes()).slice(-2),
s:t.getSeconds(),
ss:("0" + t.getSeconds()).slice(-2),
w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()]
};
return str.replace(/([a-z]+)/ig,function($1){return obj[$1]});
}
格式说明
yyyy: 年份
yy: 年份
MM: 月份,补满两位
M: 月份
dd: 日期,补满两位
d: 日期
HH: 24制小时,补满两位
H: 24制小时
hh: 12制小时,补满两位
h: 12制小时
mm: 分钟,补满两位
m: 分钟
ss: 秒,补满两位
s: 秒
w: 星期