function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
console.log(fmt)
fmt = fmt.replace(RegExp.$1,(date.getFullYear()+'').substr(4-RegExp.$1.length))
console.log(/(y+)/)
console.log((RegExp.$1).length)
console.log(fmt)
console.log(RegExp.$1)
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
console.log(str)
console.log(RegExp.$1.length)
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length ===1) ? str : padLeftZero(str))
console.log(fmt)
}
}
return fmt;
};
function padLeftZero(str) {
return ('00'+str).substr(str.length);
}
formatDate(new Date(), 'yyyy-MM-dd hh:mm')
VM216:3 yyyy-MM-dd hh:mm
VM216:5 /(y+)/
VM216:6 4
VM216:7 2017-MM-dd hh:mm
VM216:8 yyyy
VM216:21 1
VM216:22 2
VM216:24 2017-01-dd hh:mm
VM216:21 12
VM216:22 2
VM216:24 2017-01-12 hh:mm
VM216:21 0
VM216:22 2
VM216:24 2017-01-12 00:mm
VM216:21 7
VM216:22 2
VM216:24 2017-01-12 00:07
"2017-01-12 00:07"
网友评论