"yesterday", "3 days ago", "in 2 hours": time phrases per language, from the browser. You pick the value and the unit. An interactive Intl.RelativeTimeFormat…
Why it matters
Almost every app writes "posted X ago" or "due in Y". Building it yourself means nested if-else, wrong grammar in other languages, and forgetting words like "yesterday" or "the day after tomorrow". RelativeTimeFormat writes the right phrase per language, as long as you give it the right value and unit.
The key idea
RelativeTimeFormat only writes; you pick the value and the unit
Common mistakes
RelativeTimeFormat writes odd numbers like "259,200 seconds ago" when given a difference in seconds RTF writes the unit you give it. You pick the unit: find the largest one that fits, divide, then format(-3, 'day').
Hardcoding if (d === -1) return 'kemarin' is wrong in other languages Use { numeric: 'auto' }: RTF gives each language’s own word (kemarin, yesterday, gestern), with no conditions of yours.
A page with many timestamps feels slow Building a new Intl.RelativeTimeFormat for every row is expensive. Build one per locale and options, then reuse its .format().
Try it in this playground
Switch numeric to 'auto' -1 day becomes 'kemarin', 1 week 'minggu depan': words only 'auto' writes.
Try a minus value, then a plus one Minus is written in the past, plus ahead. The pin moves to the other side of the ruler.
Try three units One value, three units, three phrases. The ruler shows which unit fits the time.
Try style short and narrow '3 hr. ago', '3h ago': for tight spaces, each language with its own short forms.