songcomutil
Version:
sxj工具库
22 lines (19 loc) • 481 B
JavaScript
function formatDate(date, format) {
const map = {
'M': date.getMonth() + 1,
'D': date.getDate(),
'h': date.getHours(),
'm': date.getMinutes(),
's': date.getSeconds()
};
return format.replace(/(Y+|M+|D+|h+|m+|s+)/g, (match, key) => {
let value = String(map[key[0]]);
while (value.length < key.length) {
value = '0' + value;
}
return value;
});
}
module.exports={
formatDate
}