Intl.RelativeTimeFormat.prototype.format
Formats a value and unit according to the locale and formatting options of this Intl.RelativeTimeFormat object
Syntax
relativeTimeFormat.format(value, unit)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | number | A numeric value to use in the relative time message |
| unit | string | The unit: second, minute, hour, day, week, month, quarter, year |
Return Value
A string representing the relative time
Examples
const rtf = new Intl.RelativeTimeFormat('en')
console.log(rtf.format(-1, 'week')) // '1 week ago'
console.log(rtf.format(3, 'month')) // 'in 3 months'const rtf = new Intl.RelativeTimeFormat('fr')
console.log(rtf.format(-2, 'day')) // 'il y a 2 jours'
console.log(rtf.format(1, 'year')) // 'dans 1 an'const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' })
console.log(rtf.format(0, 'day')) // 'today'
console.log(rtf.format(1, 'day')) // 'tomorrow'
console.log(rtf.format(-1, 'day')) // 'yesterday'Understanding Intl.RelativeTimeFormat.prototype.format
The Intl.RelativeTimeFormat.prototype.format method in JavaScript formats a value and unit according to the locale and formatting options of this Intl.RelativeTimeFormat object. It belongs to the Intl.RelativeTimeFormat object and is one of the most widely used methods for working with intl.relativetimeformat values in modern JavaScript and TypeScript applications.
The method signature is relativeTimeFormat.format(value, unit). It accepts 2 parameters: value, unit. When called, it returns a string representing the relative time. Understanding when and how to use format() helps you write more expressive, readable code.
Common use cases for Intl.RelativeTimeFormat.prototype.format include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like intl-relativetimeformat, intl-datetimeformat-format, intl-numberformat-format, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Intl.RelativeTimeFormat.prototype.format 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.RelativeTimeFormatCreates an Intl.RelativeTimeFormat object that enables language-sensitive relative time formatting
Intl.DateTimeFormat.prototype.formatFormats a date according to the locale and formatting options of this Intl.DateTimeFormat object
Intl.NumberFormat.prototype.formatFormats a number according to the locale and formatting options of this Intl.NumberFormat object
Related Tools
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.