File
Blob: src/client/lib/time.ts
| 1 | const MINUTE = 60_000; |
| 2 | const HOUR = 3_600_000; |
| 3 | const DAY = 86_400_000; |
| 4 | |
| 5 | export function relativeTime(dateStr: string): string { |
| 6 | const now = Date.now(); |
| 7 | const then = new Date(dateStr).getTime(); |
| 8 | const diff = now - then; |
| 9 | |
| 10 | if (diff < MINUTE) { |
| 11 | return "just now"; |
| 12 | } |
| 13 | if (diff < HOUR) { |
| 14 | const mins = Math.floor(diff / MINUTE); |
| 15 | return `${mins}m ago`; |
| 16 | } |
| 17 | if (diff < DAY) { |
| 18 | const hours = Math.floor(diff / HOUR); |
| 19 | return `${hours}h ago`; |
| 20 | } |
| 21 | if (diff < 7 * DAY) { |
| 22 | const days = Math.floor(diff / DAY); |
| 23 | return `${days}d ago`; |
| 24 | } |
| 25 | |
| 26 | return new Intl.DateTimeFormat(undefined, { |
| 27 | month: "short", |
| 28 | day: "numeric", |
| 29 | }).format(new Date(dateStr)); |
| 30 | } |
| 31 | |
| 32 | export function fullDate(dateStr: string): string { |
| 33 | return new Date(dateStr).toLocaleString(); |
| 34 | } |
| 35 | |
| 36 | export function formatCountdown(expiresAt: string): string { |
| 37 | const diff = new Date(expiresAt).getTime() - Date.now(); |
| 38 | |
| 39 | if (diff <= 0) { |
| 40 | return "Expired"; |
| 41 | } |
| 42 | |
| 43 | const hours = Math.floor(diff / HOUR); |
| 44 | const mins = Math.floor((diff % HOUR) / MINUTE); |
| 45 | |
| 46 | if (hours > 0) { |
| 47 | return `${hours}h ${mins}m remaining`; |
| 48 | } |
| 49 | return `${mins}m remaining`; |
| 50 | } |