0

I just want to display the default format to like this yyyy/mm/dd hh:mm:ss rather than Wed Mar 04 2020 18:18:21 GMT+0530 (India Standard Time)as default format.

<!DOCTYPE html>
<html>
<body>

<h2>Function to diplay time</h2>

<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me</button>

<p id="demo"></p>

</body>
</html> 
GaganSailor
  • 435
  • 1
  • 4
  • 16
  • Use `toLocaleDateString`. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString – Ben Aston Mar 04 '20 at 12:58
  • This is not what I am looking for. – GaganSailor Mar 05 '20 at 10:28
  • Do you want to format the string produced when calling `Date()` without `new`? According to the spec, the format is fixed unfortunately. See https://www.ecma-international.org/ecma-262/10.0/index.html#sec-date-constructor-date and https://www.ecma-international.org/ecma-262/10.0/index.html#sec-todatestring. – Ben Aston Mar 05 '20 at 10:49
  • I just simply want to format the output as I mentioned above. This is the output format which I want yyyy/mm/dd hh:mm:ss – GaganSailor Mar 05 '20 at 10:56
  • That will be helpful for me if could do that in code snippet – GaganSailor Mar 05 '20 at 12:46
  • You have to build it yourself in raw JavaScript: https://codepen.io/benaston/pen/JjdrpLo?editors=0012. Otherwise: use a library. – Ben Aston Mar 05 '20 at 13:38
  • I have corrected the top answer here, so that it solves your issue: https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date# – Ben Aston Mar 05 '20 at 14:08

1 Answers1

1

function getDate() {
        var currentDate = new Date();
        document.getElementById("demo").innerHTML =
          [
            currentDate.getFullYear(),
            currentDate.getMonth() + 1,
            currentDate.getDate()
          ].join("/") +
          " " +
          [
            currentDate.getHours(),
            currentDate.getMinutes(),
            currentDate.getSeconds()
          ].join(":");
      }
<!DOCTYPE html>
<html>
<body>

<h2>Function to diplay time</h2>

<button type="button"

<!-- begin snippet: js hide: false console: true babel: false -->
onclick="document.getElementById('demo').innerHTML = Date()">
Click me</button>

<p id="demo"></p>

</body>
</html>