feat(format): add formatRelativeTime (just now, Xm/Xh/Xd ago, MM/DD/YYYY)

This commit is contained in:
Tyler
2026-06-17 14:47:01 -06:00
parent 37998aad46
commit cbd6eda640
2 changed files with 60 additions and 0 deletions
+24
View File
@@ -36,4 +36,28 @@ export function formatTime(date: string | Date | null | undefined): string {
hour: "2-digit",
minute: "2-digit",
});
}
/**
* "Just now" / "5m ago" / "2h ago" / "yesterday" / "3d ago" / MM/DD/YYYY.
*
* Used by the v2 admin Orders list (CardList time-ago badge) and any
* other place that needs a humanized, glanceable timestamp without
* pulling in a date library. Buckets are deliberately coarse — anything
* past a week is shown as the absolute date (MM/DD/YYYY) via formatDate.
*/
export function formatRelativeTime(input: string | Date | number): string {
const date = input instanceof Date ? input : new Date(input);
if (isNaN(date.getTime())) return "—";
const diffMs = Date.now() - date.getTime();
const diffSec = Math.round(diffMs / 1000);
if (diffSec < 45) return "just now";
const diffMin = Math.round(diffSec / 60);
if (diffMin < 60) return `${diffMin}m ago`;
const diffHr = Math.round(diffMin / 60);
if (diffHr < 24) return `${diffHr}h ago`;
const diffDay = Math.round(diffHr / 24);
if (diffDay === 1) return "yesterday";
if (diffDay < 7) return `${diffDay}d ago`;
return formatDate(date);
}