JavaScript Intl.RelativeTimeFormat Object
Intl.RelativeTimeFormat formats relative times, such as 3 days ago or in 2 hours, in any language.
Description
Intl.RelativeTimeFormat produces phrases describing a time relative to now, such as "2 days ago" or "in 3 weeks", correctly translated and pluralized for the locale. Call format(value, unit) with a positive or negative number and a unit such as 'second', 'minute', 'hour', 'day', 'week', 'month' or 'year'.
With numeric: 'auto', it uses words like "yesterday" and "next week" where the language has them. You still have to work out the difference and choose the unit yourself.
The style option chooses between long ("in 3 months"), short ("in 3 mo.") and narrow forms, and formatToParts() splits the result so you can style the number separately. It doesn't decide which unit to use; a common approach is to compare the difference in seconds against thresholds for minutes, hours, days and so on, and pick the largest unit that fits.
Creating
new Intl.RelativeTimeFormat(locales, options)Example
Browser Support
The following information will show you the current browser support for the Intl.RelativeTimeFormat object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





