<script type="text/javascript">
function dateFormat(dt, formater) { //author: meizz
if (dt) {
var date;
var arrDate = new Array();
var arrTime = new Array();
if ((typeof dt) == 'string') {
if (dt.indexOf('T') != -1) {
dt = dt.replace(/T/g, ' ');
}
var item = dt.split(' ');
if (dt.indexOf('-') != -1) {
arrDate = item[0].split('-');
arrTime = item[1].split(':');
} else if (dt.indexOf('/') != -1) {
arrDate = item[0].split('/');
arrTime = item[1].split(':');
}
if (arrDate && arrTime) {
date = new Date(
parseInt(arrDate[0]),
parseInt(arrDate[1]),
parseInt(arrDate[2]),
parseInt(arrTime[0]),
parseInt(arrTime[1]),
parseInt(arrTime[2])
);
}
} else {
date = new Date(dt)
}
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(formater))
formater = formater.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(formater))
formater = formater.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[
k]).length)));
return formater;
}
return "";
}
console.log(dateFormat('2020-07-17 09:10:52', 'yyyy-MM-dd hh:mm:ss'))
console.log(dateFormat('2020-06-01 18:34:39:000000', 'yyyy-MM-dd hh:mm:ss'))
console.log(dateFormat('2020-07-17T18:02:38.767+0800', 'yyyy-MM-dd hh:mm:ss'))
console.log(dateFormat(1594999335541, 'yyyy-MM-dd hh:mm:ss'))
</script>