Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

JavaScript | Intl.DateTimeFormat.prototype.formatToParts() Method

  • Last Updated : 31 Mar, 2020
Geek Week

The Intl.DateTimeFormat.prototype.formatToParts() method is an inbuilt method in JavaScript which allows locale-aware formatting of strings produced by DateTimeFormat formatters.

Syntax:

dateTimeFormat.formatToParts( date )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • date: It is an optional parameter and it holds the date to format.

Return value: This method returns an Array of objects containing the formatted date in parts.

Below examples illustrate the Intl.DateTimeFormat.prototype.formatToParts() method in JavaScript:



Example 1:




let geeks = {month: 'numeric', day: 'numeric', year: "numeric"};
let result =  new Intl.DateTimeFormat("en-u-ca-chinese", geeks);
let datetime = Date.UTC(2012, 11, 17, 3);
let val = result.formatToParts(datetime);
console.log(val[0]);
console.log(val[1]);
console.log(val[2]);
console.log(val[3]);

Output:

Object { type: "month", value: "11" }
Object { type: "literal", value: "/" }
Object { type: "day", value: "5" }
Object { type: "literal", value: "/" }

Example 2:




let date = new Intl.DateTimeFormat("hi");
let val = date.formatToParts(Date.UTC(2012, 11, 17, 3, 0, 42));
console.log(val[0]);
console.log(val[1]);
console.log(val[2]);
console.log(val[3]);
console.log(val[4]);

Output:

Object { type: "day", value: "17" }
Object { type: "literal", value: "/" }
Object { type: "month", value: "12" }
Object { type: "literal", value: "/" }
Object { type: "year", value: "2012" }

Supported Browsers: The browsers supported by Intl.DateTimeFormat.prototype.formatToParts() method are listed below:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :