anywhen — dates & times: Dates and times the way your reader expects them: written out, shortened, or just 3 hours ago.
▸about anywhen
Formats a date as the string a reader expects — "yesterday, 2:35 PM", "3 hours ago", "5 Feb 2016" — choosing the form from how far away the date is.
- wraps
- Intl.DateTimeFormat + Intl.RelativeTimeFormat
- install
- npm install anywhen
- runtime
- Node 18+, every modern browser, Deno, Bun, edge runtimes.
- docs
- anyfamily.site/docs/anywhen
- playground
- run the family on StackBlitz
why
Relative time by hand is a threshold table plus a per-locale plural mess; dayjs and date-fns ship locale files and a hundred functions to get one string. anywhen is one call, ~1.4 kB, every locale the runtime knows, and SSR-stable when you pin `now`.
usage
anywhen(date) // "yesterday, 2:35 PM"
anywhen(date, { mode: "relative" }) // "3 hours ago"
anywhen(date, { mode: "absolute", locale: "ja" }) // "2016年2月5日"
anywhen(date, { locale: "en", now: requestTime }) // SSR-safe: fixed anchor
anywhen.parts(date, { mode: "relative" }) // [{ type: "integer", value: "3", unit: "hour" }, …]
anywhen.range(from, to, { format: { day: "numeric", month: "short" } }) // "Sep 12 – 15"not for
Parsing dates, date arithmetic, or custom pattern strings like DD/MM/YYYY — use a date library for those and hand the result here.