How to format a JavaScript date?



JavaScript date follows the formatting formats −

Date Type
Format
ISO Date
"2017-11-29" (The International Standard)
Short Date
"11/29/2017"
Long Date
"29 Nov 2017"
Full Date
"Wednesday November 29 2017"

Example

Here’s how you can display current date −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <p>Click below to get the current date:</p>
      <button onclick="display()">Display Date</button>
      <p id="test"></p>
      <script>
         function display() {
            var date = new Date();
            document.getElementById("test").innerHTML = date;
         }
      </script>
   </body>
</html>

Advertisements