Open In App

JavaScript Intl RelativeTimeFormat() Constructor

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Intl RelativeTimeFormat() Constructor is used for creating Intl.RelativeTimeFormat object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError.

Syntax:

new Intl.RelativeTimeFormat(loc, opt)

Parameters: It has two parameters both are optional.

  • loc: It is a String or an array of Strings that contains the general form and interpretation of arguments
  • opt: It is an object which contains properties like localeMatcher and style and numeric.

Return Value: An Intl.RelativeFormat object.

Below examples illustrate the JavaScript Intl RelativeTimeFormat() Constructor:

Example 1: This example creates a basic RelativeTimeFormat Object and uses it to format the time.

Javascript




const timeFormat = new Intl.RelativeTimeFormat("en",{
    localeMatcher: "lookup",
    numeric: "always",
    style: "short",
});
console.log(timeFormat.format(-2,"year"));
console.log(timeFormat.format(-3,"week"));


Output: 

2 yr. ago
VM162:8 3 wk. ago

Example 2: This example uses RelativeTimeFormat Object with auto property

Javascript




const timeFormat = new Intl.RelativeTimeFormat("en",{
    localeMatcher: "lookup",
    numeric: "auto",
    style: "long",
});
  
console.log(timeFormat.format(2,"day"));
console.log(timeFormat.format(-3,"day"));


Output:

in 2 days
3 days ago

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

We have a complete list of JavaScript Intl methods to check please go through, the JavaScript Intl Reference article


Similar Reads

JavaScript Intl Collator() Constructor
JavaScript Intl.Collator() constructor is used to create an Intl.Collator object. This constructor can be called with or without the new keyword. Syntax: new Intl.Collator(loc, opt) Parameters: This constructor takes two parameters and both are optional. loc: This is a String or an array of Strings with the following values allowed:co: Specifies th
1 min read
JavaScript Intl DisplayNames() Constructor
JavaScript Intl.DisplayNames Constructor is used for creating Intl.DisplayNames object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError Syntax: new Intl.DisplayNames(loc, opt) Parameters: It has two parameters both are optional. loc: It is a String or an array of Strin
2 min read
JavaScript Intl DateTimeFormat() Constructor
JavaScript Intl.DateTimeFormat Constructor is used for creating Intl.DateTimeFormat objects. This constructor can be called with or without the new keyword Syntax: Intl.DateTimeFormat(loc, opt) new Intl.DateTimeFormat(loc, opt) Parameter: This constructor has two methods and both are optional. loc: This is a String or an array of Strings with the f
2 min read
JavaScript Intl ListFormat() Constructor
JavaScript Intl.ListFormat() Constructor is used for creating Intl.ListFormat object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError. Syntax: new Intl.ListFormat(loc, opt) Parameters: It has two parameters both are optional. loc: It is a String or an array of Strings
1 min read
JavaScript Intl PluralRules() Constructor
JavaScript Intl PluralRules() Constructor is used for creating Intl.PluralRules object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError. Syntax: new Intl.PluralRules(loc, opt) Parameters: loc: It is a String or an array of Strings that contains the general form and int
1 min read
JavaScript Intl Segementer() Constructor
JavaScript Intl Segmenter() Constructor is used for creating Intl.Segmenter object. This constructor is created using the new keyword. If we create the constructor without the new keyword it will give a TypeError. Syntax: new Intl.Segmenter(loc, opt) Parameters: It has two parameters both are optional. loc: It is a String or an array of Strings tha
2 min read
JavaScript Intl.NumberFormat() Constructor
The Javascript Intl.NumberFormat() constructor is used to create Intl.NumberFormat() object that enables language-sensitive number formatting. This constructor is created with or without a new keyword both create a new Intl.NumberFormat instance. Syntax: new Intl.NumberFormat() new Intl.NumberFormat([locales[, options]]) Parameters: Locales: It con
2 min read
JavaScript Intl.getCanonicalLocales() Method
The Intl.getCanonicalLocales() method in JavaScript is a Standard built-in object which returns an array containing the canonical locale names. Elements will be checked for structural validity and duplicates will be removed. Syntax: Intl.getCanonicalLocales(locales) Parameters: This method accepts a single parameter as mentioned above and described
1 min read
JavaScript Intl ListFormat resolvedOptions() Method
The Intl.ListFormat.prototype.resolvedOptions() method is an inbuilt method in JavaScript that returns a new object that has characteristics that correspond to the locale and style formatting choices that were determined during the creation of the existing ListFormat object Syntax: listFormat.resolvedOptions() Parameters: This method does not accep
1 min read
JavaScript Intl ListFormat formatToParts() Method
The Intl.ListFormat.prototype.formatToParts() method is an inbuilt method in JavaScript that returns an Array of objects representing the different components that can be used to format a list of values in a locale-aware fashion. Syntax: Intl.ListFormat.prototype.formatToParts(list) Parameters: This method accepts a single parameter as mentioned ab
2 min read
Article Tags :