Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Intl DateTimeFormat formatToParts() Method

JavaScript Intl DateTimeFormat formatToParts() Method

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.

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

Example 1: In this example, we will format the date and print its type and value using the Intl.DateTimeFormat.prototype.formatToParts() method in JavaScript.

javascript




<script>
    let neveropen = {month: 'numeric', day: 'numeric', year: "numeric"};
    let result =  new Intl.DateTimeFormat("en-u-ca-chinese", neveropen);
    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]);
</script>


Output: 

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

Example 2: In this example, we will format the date and print its type and value using the Intl.DateTimeFormat.prototype.formatToParts() method in JavaScript.

javascript




<script>
    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]);
</script>


Output: 

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

We have a complete list of Javascript Intl methods, to check those please go through the Javascript Intl Complete Reference article.

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

  • Google Chrome 57 and above
  • Edge 18 and above
  • Firefox 51 and above
  • Opera 44 and above
  • Safari 11 and above

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments