JavaScript toLocaleDateString() | Get Date as String using Locale Conventions

The JavaScript toLocaleDateString() method is used to get the date (using the locale conventions) as string. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      const d = new Date();
      let dateLocale = d.toLocaleDateString();
      document.getElementById("xyz").innerHTML = dateLocale;
   </script>

</body>
</html>
Output

Please note: In above date, the first value represents the month number of the year, second value represents the day of the month, whereas the last value represents the year.

JavaScript toLocaleDateString() Syntax

The syntax of the toLocaleDateString() method in JavaScript is:

x.toLocaleDateString()

where x must be an object of the Date() constructor.

The toLocaleDateString() method returns a string which will be the date using the locale conventions.

Please note: To get the time based on local settings, use toLocaleTimeString().

Please note: To get the complete date based on local settings, use toLocaleString().

Please note: To display date in format dd-mm-yyyy, refer to its separate example.

Please note: To display time in format hh:mm:ss, refer to its separate example.

Please note: To display time in format hh:mm:ss AM/PM, refer to its separate example.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋