-2

For eg-

<body>
<p id="demo"></p>
<script>
var x = new Date();
document.getElementById("demo").innerHTML=x;
</script>
</body>

And,

<body>
<p id="demo"></p>
<script>
var x = Date();
document.getElementById("demo").innerHTML=x;
</script>
</body>

The result is same.But what it means when we use 'new'?

  • 1
    More information about JavaScript new keyworkd in this post : http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript. – OrcusZ Apr 03 '17 at 12:30

1 Answers1

-2

Date()

With this you call a function called Date(). It doesn't accept any arguments and returns a string representing the current date and time.

new Date()

With this you're creating a new instance of Date.

You can use only the following constructors:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

So, use 2010-08-17 12:09:36 as parameter to constructor is not allowed.

See w3schools.

Andy Taylor
  • 102
  • 7