全新popiart网站 react项目重构
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

35 lines
961 B

function pad2(v: number): string {
return String(v).padStart(2, "0");
}
/**
* 按本地时区格式化日期(仅日期部分)。
* 支持 token:YYYY、YY、MM、M、DD、D
*/
export function formatLocalDate(ms: number, format: string = "YYYY-MM-DD HH:mm:ss"): string {
const d = new Date(ms);
if (Number.isNaN(d.getTime())) return "";
const year = d.getFullYear();
const month = d.getMonth() + 1;
const day = d.getDate();
const hour = d.getHours();
const minute = d.getMinutes();
const second = d.getSeconds();
const tokenMap: Record<string, string> = {
YYYY: String(year),
YY: String(year).slice(-2),
MM: pad2(month),
M: String(month),
DD: pad2(day),
D: String(day),
HH: pad2(hour),
H: String(hour),
mm: pad2(minute),
m: String(minute),
ss: pad2(second),
s: String(second),
};
return format.replace(/YYYY|YY|MM|M|DD|D|HH|H|mm|m|ss|s/g, (token) => tokenMap[token] ?? token);
}