feat: 添加评论时间格式化功能,优化评论项显示,支持显示编辑时间

This commit is contained in:
2025-09-10 12:34:08 +08:00
parent 09c024ccbb
commit c6e89c0b86
5 changed files with 88 additions and 9 deletions

View File

@ -0,0 +1,52 @@
function getAgoString(diff: number, unitI18n: { secondsAgo: string; minutesAgo: string; hoursAgo: string; daysAgo: string; }): string {
let value: number, unit: string;
if (diff < 60 * 1000) {
value = Math.floor(diff / 1000);
unit = unitI18n.secondsAgo;
return `${value}${unit}`;
} else if (diff < 60 * 60 * 1000) {
value = Math.floor(diff / (60 * 1000));
unit = unitI18n.minutesAgo;
return `${value}${unit}`;
} else if (diff < 24 * 60 * 60 * 1000) {
value = Math.floor(diff / (60 * 60 * 1000));
unit = unitI18n.hoursAgo;
return `${value}${unit}`;
} else {
value = Math.floor(diff / (24 * 60 * 60 * 1000));
unit = unitI18n.daysAgo;
return `${value}${unit}`;
}
}
export function formatDateTime({
dateTimeString,
locale,
convertShortAgo,
convertShortAgoDuration = 3 * 24 * 60 * 60 * 1000,
unitI18n = { secondsAgo: "s ago", minutesAgo: "m ago", hoursAgo: "h ago", daysAgo: "d ago" }
}: {
dateTimeString: string;
locale: string;
convertShortAgo?: boolean;
convertShortAgoDuration?: number;
unitI18n?: { secondsAgo: string; minutesAgo: string; hoursAgo: string; daysAgo: string; };
}): string {
const date = new Date(dateTimeString);
const now = new Date();
const diff = now.getTime() - date.getTime();
if (convertShortAgo && diff >= 0 && diff < convertShortAgoDuration) {
return getAgoString(diff, unitI18n);
}
return date.toLocaleString(locale, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
});
}