Intl.RelativeTimeFormat
Creates an Intl.RelativeTimeFormat object that enables language-sensitive relative time formatting
Syntax
new Intl.RelativeTimeFormat(locales?, options?)Parameters
| Parameter | Type | Description |
|---|---|---|
| locales | string | string[] | A BCP 47 language tag or array of tags |
| options | Intl.RelativeTimeFormatOptions | Options: numeric, style |
Return Value
An Intl.RelativeTimeFormat object with a format() method
Examples
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
console.log(rtf.format(-1, 'day')) // 'yesterday'
console.log(rtf.format(2, 'day')) // 'in 2 days'const rtf = new Intl.RelativeTimeFormat('en', { style: 'short' })
console.log(rtf.format(-3, 'hour')) // '3 hr. ago'
console.log(rtf.format(5, 'minute')) // 'in 5 min.'function timeAgo(date: Date): string {
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
const diff = (date.getTime() - Date.now()) / 1000
if (Math.abs(diff) < 60) return rtf.format(Math.round(diff), 'second')
if (Math.abs(diff) < 3600) return rtf.format(Math.round(diff / 60), 'minute')
if (Math.abs(diff) < 86400) return rtf.format(Math.round(diff / 3600), 'hour')
return rtf.format(Math.round(diff / 86400), 'day')
}Understanding Intl.RelativeTimeFormat
The Intl.RelativeTimeFormat method in JavaScript creates an Intl.RelativeTimeFormat object that enables language-sensitive relative time formatting. It belongs to the Intl object and is one of the most widely used methods for working with intl values in modern JavaScript and TypeScript applications.
The method signature is new Intl.RelativeTimeFormat(locales?, options?). It accepts 2 parameters: locales, options. When called, it returns an intl.relativetimeformat object with a format() method. Understanding when and how to use RelativeTimeFormat() helps you write more expressive, readable code.
Common use cases for Intl.RelativeTimeFormat include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like intl-datetimeformat, intl-numberformat, intl-listformat, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Intl.RelativeTimeFormat is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Intl.DateTimeFormatCreates an Intl.DateTimeFormat object that enables language-sensitive date and time formatting
Intl.NumberFormatCreates an Intl.NumberFormat object that enables language-sensitive number formatting
Intl.ListFormatCreates an Intl.ListFormat object that enables language-sensitive list formatting
More Intl Methods
Other methods in the Intl object
Related Tools
More Intl Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.