Intl.RelativeTimeFormat

Formats relative time in the chosen language, e.g. "in 3 days", "2 hours ago", or "yesterday".

Returns
A formatter object with format() and formatToParts() methods.
On this page

Example

#
JavaScript
const rtf = new Intl.RelativeTimeFormat('en-US')

console.log(rtf.format(-1, 'day'))
console.log(rtf.format(3, 'day'))
console.log(rtf.format(-2, 'hour'))
console.log(rtf.format(-5, 'minute'))
Console
1 day ago
in 3 days
2 hours ago
5 minutes ago

Definition and usage

#

Intl.RelativeTimeFormat turns a number and a time unit into a natural description. format(-2, "hour") gives "2 hours ago", and format(3, "day") gives "in 3 days". A negative number means the past, a positive one the future. The formatter picks the correct word forms for the given language on its own.

The available units are "second", "minute", "hour", "day", "week", "month", "quarter", and "year". The numeric: "auto" option uses words instead of numbers where the language has them: "yesterday", "today", "tomorrow", "next week". The style option, with the values "long", "short", and "narrow", shortens the text, e.g. to "in 3 hr.".

The formatter doesn’t calculate the difference between dates. You compute how many seconds, minutes, or days have passed, and choose the unit yourself. A short function usually does this, checking the size of the difference and picking the largest sensible unit, as in the time-since-publication example below.

Syntax

#
Syntax
new Intl.RelativeTimeFormat(locales)
new Intl.RelativeTimeFormat(locales, options)

formatter.format(value, unit)

Parameters

#
  • locales

    optional, string or array

    A locale code, e.g. "en-US" or "pl-PL". If omitted, it means the browser’s language.
  • options

    optional object

    Settings: numeric ("always" or "auto") and style ("long", "short", or "narrow").

More examples

#
Words instead of numbers
JavaScript
const rtf = new Intl.RelativeTimeFormat('en-US', { numeric: 'auto' })

for (const days of [-2, -1, 0, 1, 2]) {
  console.log(rtf.format(days, 'day'))
}
console.log(rtf.format(1, 'week'))
console.log(rtf.format(-3, 'day'))
Console
2 days ago
yesterday
today
tomorrow
in 2 days
next week
3 days ago

English only has special words for yesterday, today, and tomorrow. Two days or more in either direction, and the formatter falls back to the numeric form.

Time since a post was published
JavaScript
const rtf = new Intl.RelativeTimeFormat('en-US', { numeric: 'auto' })

function timeAgo(date, now) {
  const seconds = Math.round((date - now) / 1000)
  const units = [
    ['day', 86400],
    ['hour', 3600],
    ['minute', 60]
  ]
  for (const [unit, size] of units) {
    if (Math.abs(seconds) >= size) {
      return rtf.format(Math.round(seconds / size), unit)
    }
  }
  return rtf.format(seconds, 'second')
}

const now = new Date('2026-09-29T12:00:00Z')

console.log(timeAgo(new Date('2026-09-29T11:15:00Z'), now))
console.log(timeAgo(new Date('2026-09-29T07:00:00Z'), now))
console.log(timeAgo(new Date('2026-09-28T09:00:00Z'), now))
console.log(timeAgo(new Date('2026-09-29T11:59:50Z'), now))
Console
45 minutes ago
5 hours ago
yesterday
10 seconds ago
A shorter style and other languages
JavaScript
const short = new Intl.RelativeTimeFormat('en-US', { style: 'short' })
const pl = new Intl.RelativeTimeFormat('pl-PL', { numeric: 'auto' })

console.log(short.format(3, 'hour'))
console.log(short.format(-2, 'month'))
console.log(pl.format(-1, 'day'))
console.log(pl.format(2, 'week'))
Console
in 3 hr.
2 mo. ago
wczoraj
za 2 tygodnie

Browser support

#

Widely available · since 2020

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Turn on numeric: "auto" to show the natural "yesterday" instead of "1 day ago", and "tomorrow" instead of "in 1 day".
  • Watch the sign of the number: a negative value is the past ("ago"), a positive value is the future ("in"). Subtracting dates in the wrong order is the most common mistake.
  • For exact deadlines, like a project due date, use Intl.DateTimeFormat. Relative time works best for fresh events: notifications, comments, and friends’ activity.

Related entries

#

Spotted a mistake or missing an example? Write to us.