function _replace(date, str) {
var time = new Date(date);
var y = time.getFullYear();
var M = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
var result = str.replace(/(y+)/g, y).replace(/(M+)/g, M).replace(/(d+)/g, d).replace(/(h+)/g, h).replace(/(m+)/g, m).replace(/(s+)/g, s);
return result;
}
使用正则表达式来替换给定日期格式
console.log(_replace(1654343872384, "yyyy-MM-dd hh:mm:ss"));
输出:2022-6-4 19:57:52