new Date()
takes an ordinal and returns a Date
object.
What does Date()
do, and how come it gives a different time?
>>> new Date(1329429600000)
Date {Fri Feb 17 2012 00:00:00 GMT+0200 (القدس Standard Time)}
>>> Date(1329429600000)
"Tue Mar 06 2012 15:29:58 GMT+0200 (Jerusalem Standard Time)"
Date() returns an implementation dependent string representing the current date and time. new Date() returns a Date object that represents the current date and time. ;-) Yeah, I know.
It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.
Calling the Date() function (without the new keyword) returns a string representation of the current date and time, exactly as new Date().
Use new Date() to generate a new Date object containing the current date and time. This will give you today's date in the format of mm/dd/yyyy.
From the specs:
When
Date
is called as a function rather than as a constructor, it returns a String representing the current time (UTC).
and:
When
Date
is called as part of anew
expression, it is a constructor: it initialises the newly created object.
So, new Date(...)
returns an object such that obj instanceof Date
is true, whereas Date(...)
basically returns the same as new Date().toString()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With