Something went wrong. Try again.
Logiverse library. (beta)
Something went wrong. Try again.
705 B · 24 lines
TypeScript
at main
12345678910111213141516171819202122232425import type { Context } from "../index.js"
const units = { year: 24 * 60 * 60 * 1000 * 365, month: 24 * 60 * 60 * 1000 * 365 / 12, day: 24 * 60 * 60 * 1000, hour: 60 * 60 * 1000, minute: 60 * 1000, second: 1000} as const
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
export function RelativeTime({ timestamp }: Context): { relativeTime: string | null } { const elapsed = timestamp.getTime() - new Date().getTime()
for (const u in units) if (Math.abs(elapsed) > units[u as keyof typeof units] || u == 'second') return { relativeTime: rtf.format(Math.round(elapsed / units[u as keyof typeof units]), u as keyof typeof units) }
return { relativeTime: null }}