| 1 | const format = new Intl.RelativeTimeFormat('en', { style: 'short' }) |
| 2 | const units = [ |
| 3 | ['year', 31_536_000], |
| 4 | ['month', 2_592_000], |
| 5 | ['day', 86_400], |
| 6 | ['hour', 3_600], |
| 7 | ['minute', 60], |
| 8 | ] as const |
| 9 | |
| 10 | // ago says how long ago a time was: "3 hr. ago", "just now". |
| 11 | export function ago(iso: string) { |
| 12 | const seconds = (Date.parse(iso) - Date.now()) / 1000 |
| 13 | for (const [unit, size] of units) { |
| 14 | if (-seconds >= size) { |
| 15 | return format.format(Math.round(seconds / size), unit) |
| 16 | } |
| 17 | } |
| 18 | return 'just now' |
| 19 | } |
| 20 | |
| 21 | export const month = (iso: string) => |
| 22 | new Date(iso).toLocaleDateString('en', { |
| 23 | month: 'long', |
| 24 | year: 'numeric', |
| 25 | }) |