Intl.RelativeTimeFormat Overview
What you'll learn: You'll understand what relative time formatting is and why Intl.RelativeTimeFormat gives you locale-aware, human-friendly time descriptions.
What Is Relative Time Formatting?
When you show time to users, raw numbers like "3 days" or "2 hours ago" need careful language handling. Different languages express these differently: English says "in 2 days," Spanish says "dentro de 2 días," and Japanese uses "2 日後." Hardcoding these strings means your app only works in one language.
Relative time formatting means displaying time differences in natural, conversational language relative to now—like "5 minutes ago," "tomorrow," or "in 3 weeks."
Why Intl.RelativeTimeFormat Exists
Just like Intl.DateTimeFormat handles locale-specific date formatting and Intl.NumberFormat handles number formatting, Intl.RelativeTimeFormat solves the problem of internationalized relative time descriptions.
Without it, you'd need to:
- Manually write translation tables for every language
- Handle plural rules (1 day vs 2 days) per locale
- Manage past vs future phrasing differences ("ago" vs "in")
- Account for cultural conventions
Intl.RelativeTimeFormat does all this automatically, using the same locale system you've already seen with other Intl APIs.
The Mental Model
Think of it like a translator that takes two inputs—a number (how many) and a unit (days, hours, weeks)—and outputs a localized phrase appropriate for that locale. It knows whether you're talking about the past or future based on the number's sign.
Key Takeaway: Intl.RelativeTimeFormat transforms numeric time differences into human-readable, locale-appropriate phrases like "3 hours ago" or "in 2 weeks," automatically handling translations, pluralization, and past/future phrasing across languages.