/** * Shared date/time formatting utilities for Route Commerce. * All dates are stored as TIMESTAMPTZ in PostgreSQL (UTC). * Display dates in MM/DD/YYYY format in US locale. */ export function formatDate(date: string | Date | null | undefined): string { if (!date) return "—"; const d = typeof date === "string" ? new Date(date) : date; if (isNaN(d.getTime())) return "—"; return d.toLocaleDateString("en-US", { month: "2-digit", day: "2-digit", year: "numeric", }); } export function formatDateTime(date: string | Date | null | undefined): string { if (!date) return "—"; const d = typeof date === "string" ? new Date(date) : date; if (isNaN(d.getTime())) return "—"; return d.toLocaleDateString("en-US", { month: "2-digit", day: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", }); } export function formatTime(date: string | Date | null | undefined): string { if (!date) return "—"; const d = typeof date === "string" ? new Date(date) : date; if (isNaN(d.getTime())) return "—"; return d.toLocaleTimeString("en-US", { 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); }