Fork Horse repository, listed by its owner. Publisher snapshot.
packages/forkhorse-dashboard/src/time.ts
Snapshot at 34e3175
1/** Relative copy shared by activity, receipt history and running-check captions. */
2export function relativeTime(
3 value: string,
4 now = Date.now(),
5 maxUnit: "day" | "hour" = "day",
6): string {
7 const elapsed = Math.max(0, now - Date.parse(value));
8 if (!Number.isFinite(elapsed)) return "";
9 if (elapsed < 60_000) return "Just now";
10 if (elapsed < 3_600_000) return `${Math.floor(elapsed / 60_000)} min ago`;
11 if (elapsed < 86_400_000 || maxUnit === "hour")
12 return `${Math.floor(elapsed / 3_600_000)} hr ago`;
13 const days = Math.floor(elapsed / 86_400_000);
14 return `${days} ${days === 1 ? "day" : "days"} ago`;
15}
16