Intl.RelativeTimeFormat
You'll learn how to display human-friendly relative times like "in 3 days" or "2 hours ago" automatically localized to any language.
What Is Intl.RelativeTimeFormat?
The Intl.RelativeTimeFormat object lets you format relative time expressions in a locale-aware way. Instead of manually writing "3 days ago" or "dans 3 jours" (French), you provide a number and a unit, and the browser generates the correct phrasing for the user's language.
How It Works
First, create a formatter with the user's locale:
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
Then format relative times by specifying a value (positive for future, negative for past) and a unit:
rtf.format(-1, 'day'); // "yesterday"
rtf.format(2, 'day'); // "in 2 days"
rtf.format(-3, 'hour'); // "3 hours ago"
Numeric vs. Auto Style
The numeric option controls output style:
const numeric = new Intl.RelativeTimeFormat('en', { numeric: 'always' });
numeric.format(-1, 'day'); // "1 day ago"
const auto = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
auto.format(-1, 'day'); // "yesterday"
With 'auto', special cases like "yesterday," "today," or "tomorrow" appear naturally.
Supported Units
Valid units include: 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second'.
Different Locales in Action
const french = new Intl.RelativeTimeFormat('fr');
french.format(-2, 'day'); // "il y a 2 jours"
const spanish = new Intl.RelativeTimeFormat('es');
spanish.format(5, 'hour'); // "dentro de 5 horas"
Key Takeaway: Intl.RelativeTimeFormat automatically generates locale-specific relative time strings, making your app feel native to users worldwide without manual translation tables.