Intl.RelativeTimeFormat

Intl.RelativeTimeFormat.prototype.format

Formats a value and unit according to the locale and formatting options of this Intl.RelativeTimeFormat object

Syntax

JavaScript
relativeTimeFormat.format(value, unit)

Parameters

ParameterTypeDescription
valuenumberA numeric value to use in the relative time message
unitstringThe unit: second, minute, hour, day, week, month, quarter, year

Return Value

A string representing the relative time

Examples

Basic Usage
const rtf = new Intl.RelativeTimeFormat('en')
console.log(rtf.format(-1, 'week')) // '1 week ago'
console.log(rtf.format(3, 'month')) // 'in 3 months'
Practical Example
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'
Advanced Usage
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

Related Tools

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.