js格式化 php时间戳
时间格式化
time 为php秒级时间戳
format 为时间格式
function timeFormat(time, format = 'Y-m-d H:i:s') { if (isNaN(time) || time < 0) { throw new Error('Invalid time value'); } const currentTime = new Date(time * 1000); // 假设time是UTC时间戳 const data = { Y: currentTime.getFullYear(), m: String(currentTime.getMonth() + 1).padStart(2, '0'), d: String(currentTime.getDate()).padStart(2, '0'), H: String(currentTime.getHours()).padStart(2, '0'), i: String(currentTime.getMinutes()).padStart(2, '0'), s: String(currentTime.getSeconds()).padStart(2, '0') }; // 使用模板字符串和replace()方法进行格式化 return format.replace(/([YmdHis])/g, (match) => data[match]); }