const UNITS: ReadonlyArray = [ ['year', 365 * 24 * 60 * 60], ['month', 30 * 24 * 60 * 60], ['day', 24 * 60 * 60], ['hour', 60 * 60], ['minute', 60] ] export function formatRelativeTime(value: string, now = new Date()): string { const date = new Date(value) if (Number.isNaN(date.getTime())) return '' const elapsedSeconds = Math.max(0, Math.round((now.getTime() - date.getTime()) / 1000)) if (elapsedSeconds < 60) return 'just now' const formatter = new Intl.RelativeTimeFormat('en-US', { numeric: 'always' }) for (const [unit, seconds] of UNITS) { if (elapsedSeconds >= seconds) { return formatter.format(-Math.floor(elapsedSeconds / seconds), unit) } } return 'just now' }