/** * Render a relative time like "3 hours ago" / "in 2 weeks" in the given locale, * picking the largest sensible unit. Locale-aware via `Intl.RelativeTimeFormat`, * so no per-string i18n keys are needed. */ export function formatRelativeTime(iso: string | number | Date, locale: string): string { const diffSec = (new Date(iso).getTime() - Date.now()) / 1000; const abs = Math.abs(diffSec); const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }); if (abs < 60) return rtf.format(Math.round(diffSec), 'second'); if (abs < 3600) return rtf.format(Math.round(diffSec / 60), 'minute'); if (abs < 86400) return rtf.format(Math.round(diffSec / 3600), 'hour'); if (abs < 604800) return rtf.format(Math.round(diffSec / 86400), 'day'); if (abs < 2592000) return rtf.format(Math.round(diffSec / 604800), 'week'); if (abs < 31536000) return rtf.format(Math.round(diffSec / 2592000), 'month'); return rtf.format(Math.round(diffSec / 31536000), 'year'); }