| 1 | /** "now", "5m", "3h", "2d", or a date, for lists. */ |
| 2 | export function ago(iso: string, now = Date.now()): string { |
| 3 | const then = Date.parse(iso); |
| 4 | if (Number.isNaN(then)) return ""; |
| 5 | const seconds = Math.max(0, Math.round((now - then) / 1000)); |
| 6 | if (seconds < 60) return "now"; |
| 7 | const minutes = Math.round(seconds / 60); |
| 8 | if (minutes < 60) return `${minutes}m`; |
| 9 | const hours = Math.round(minutes / 60); |
| 10 | if (hours < 24) return `${hours}h`; |
| 11 | const days = Math.round(hours / 24); |
| 12 | if (days < 7) return `${days}d`; |
| 13 | return new Date(then).toLocaleDateString(undefined, { month: "short", day: "numeric" }); |
| 14 | } |
| 15 | |
| 16 | /** A message's time of day: "14:05". */ |
| 17 | export function clock(iso: string): string { |
| 18 | const date = new Date(iso); |
| 19 | return Number.isNaN(date.getTime()) ? "" : date.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" }); |
| 20 | } |