Skip to content
refs
History
22 lines · 689 BRaw
1const DATE = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "UTC" });
2
3export function formatDate(seconds: number | null): string {
4 return seconds ? DATE.format(new Date(seconds * 1000)) : "Never";
5}
6
7const UNITS: [string, number][] = [
8 ["year", 60 * 60 * 24 * 365],
9 ["month", 60 * 60 * 24 * 30],
10 ["day", 60 * 60 * 24],
11 ["hour", 60 * 60],
12 ["minute", 60],
13];
14
15export function formatAgo(seconds: number, now: number): string {
16 const elapsed = Math.max(0, now - seconds);
17 for (const [unit, size] of UNITS) {
18 const count = Math.floor(elapsed / size);
19 if (count >= 1) return `${count} ${unit}${count === 1 ? "" : "s"} ago`;
20 }
21 return "just now";
22}