JavaScript toLocaleString() function

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

The toLocaleString() method of JavaScript is used to convert a Date object to a string using locale settings.

The syntax is as follows −

Date.toLocaleString(locales, options)

Above, the locales parameter is the language specific format to be used −

ar-SA Arabic (Saudi Arabia)
bn-BD Bangla (Bangladesh)
bn-IN Bangla (India)
cs-CZ Czech (Czech Republic)
da-DK Danish (Denmark)
de-AT Austrian German
de-CH "Swiss" German
de-DE Standard German (as spoken in Germany)
el-GR Modern Greek
en-AU Australian English
en-CA Canadian English
en-GB British English
en-IE Irish English
en-IN Indian English
en-NZ New Zealand English
en-US US English
en-ZA English (South Africa)
es-AR Argentine Spanish
es-CL Chilean Spanish
es-CO Colombian Spanish
es-ES Castilian Spanish (as spoken in Central-Northern Spain)
es-MX Mexican Spanish
es-US American Spanish
fi-FI Finnish (Finland)
fr-BE Belgian French

Let us now implement the array. toLocaleString() method in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Products</h2>
<p>Click to display the updated product list...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
   var products = ["Electronics", "Books", "Accessories"];
   document.getElementById("test").innerHTML = products;
   function display() {
      var d = new Date();
      products.splice(3, 1, "Pet Supplies", "Footwear", "Home Appliances");
      document.getElementById("test").innerHTML = products +" on "+d.toLocaleString();
   }
</script>
</body>
</html>

Output

Click on the “Result” button above −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>Displaying Date</h2>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
   document.getElementById("test").innerHTML = "Display the current date...";
   function display() {
      var d = new Date();
      document.getElementById("test").innerHTML = d.toLocaleString();
   }
</script>
</body>
</html>

Output

Click “Result” button −

raja
Published on 17-Dec-2019 10:53:46
Advertisements