Intl.RelativeTimeFormat() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
The Intl.RelativeTimeFormat()
constructor creates Intl.RelativeTimeFormat
objects.
Syntax
Parameters
locales
Optional-
A string with a BCP 47 language tag or an
Intl.Locale
instance, or an array of such locale identifiers. The runtime's default locale is used whenundefined
is passed or when none of the specified locale identifiers is supported. For the general form and interpretation of thelocales
argument, see the parameter description on theIntl
main page.The following Unicode extension key is allowed:
nu
-
See
numberingSystem
.
This key can also be set with
options
(as listed below). When both are set, theoptions
property takes precedence. options
Optional-
An object containing the following properties, in the order they are retrieved (all of them are optional):
localeMatcher
-
The locale matching algorithm to use. Possible values are
"lookup"
and"best fit"
; the default is"best fit"
. For information about this option, see Locale identification and negotiation. numberingSystem
-
The numbering system to use for number formatting, such as
"arab"
,"hans"
,"mathsans"
, and so on. For a list of supported numbering system types, seeIntl.Locale.prototype.getNumberingSystems()
. This option can also be set through thenu
Unicode extension key; if both are provided, thisoptions
property takes precedence. style
-
The style of the formatted relative time. Possible values are:
numeric
-
Whether to use numeric values in the output. Possible values are
"always"
and"auto"
; the default is"always"
. When set to"auto"
, the output may use more idiomatic phrasing such as"yesterday"
instead of"1 day ago"
.
Exceptions
RangeError
-
Thrown if
locales
oroptions
contain invalid values.
Examples
Basic format usage
The following example shows how to create a relative time formatter using the English language.
// Create a relative time formatter in your locale
// with default values explicitly passed in.
const rtf = new Intl.RelativeTimeFormat("en", {
localeMatcher: "best fit", // other values: "lookup"
numeric: "always", // other values: "auto"
style: "long", // other values: "short" or "narrow"
});
// Format relative time using negative value (-1).
rtf.format(-1, "day"); // "1 day ago"
// Format relative time using positive value (1).
rtf.format(1, "day"); // "in 1 day"
Using the auto option
If numeric:auto
option is passed, it will produce the string yesterday
or tomorrow
instead of 1 day ago
or in 1 day
. This allows to not always have to use numeric values in the output.
// Create a relative time formatter in your locale
// with numeric: "auto" option value passed in.
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
// Format relative time using negative value (-1).
rtf.format(-1, "day"); // "yesterday"
// Format relative time using positive day unit (1).
rtf.format(1, "day"); // "tomorrow"
Specifications
Specification |
---|
ECMAScript Internationalization API Specification # sec-intl-relativetimeformat-constructor |
Browser compatibility
BCD tables only load in the browser
See also
Intl.RelativeTimeFormat
Intl
Intl.RelativeTimeFormat
on v8.dev (2018)