/

HTML / CSS / JavaScript Tutorial

JavaScript Date method: toLocaleDateString()

[this page | pdf | back links]

The toLocaleDateString() method (when applied to a JavaScript date) returns the date portion as a string, using locale-specified notation.

 

It has the following syntax (with no parameters):

 

date.toLocaleDateString()

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var d = new Date(Date.now());
document.getElementById("Example").innerHTML =
  'var d = new Date(Date.now());<br>' +
  'var x = d.toLocaleDateString();';
document.getElementById("Result").innerHTML =
  d.toLocaleDateString();
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodDateToLocaleDateString() {
  var d = new Date(Date.now()); return !!d.toLocaleDateString;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Dates


Desktop view | Switch to Mobile