new Date(null)
// Date 1970-01-01T00:00:00.000Z
How come when I type new Date(null)
in JavaScript console I'm gettingDate 1970-01-01T00:00:00.000Z
?
"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.
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.
The date object itself (as shown by your link) is the number of milliseconds since 1 January 1970 UTC - so therefore the Date itself doesn't have a timezone - it's just a number.
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.
Because the ECMAScript 2017 standard says so?
The end of ECMA section 20.3.1.1 claims:
The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0.
...which might make you say, "...but!, but!, I did not say +0, I said":
new Date(null)
Ok, so let's follow the standard on that one...
That Date constructor example goes to section 20.3.2.2, where item 3.b.iii there says:
3.b.iii: Else, let V be ToNumber(v).
ToNumber is a hyperlink, so follow that to section 7.1.3 where there is a number conversion table that shows:
Argument Type | Result
Null | +0
Therefore:
new Date(null)
Effectively becomes:
new Date(+0)
Which is why you ultimately get:
Date 1970-01-01T00:00:00.000Z
Apparently because that (Unix timestamp 0) is what a Date object is initialized to by default, eh?
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