Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why new Date(null) is returning this: Date 1970-01-01T00:00:00.000Z?

new Date(null)
// Date 1970-01-01T00:00:00.000Z

How come when I type new Date(null) in JavaScript console I'm getting
Date 1970-01-01T00:00:00.000Z?

like image 618
Baikuntha Avatar asked Mar 17 '18 17:03

Baikuntha


People also ask

What does new Date () return?

"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.

Does new Date () Return current 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.

What timezone does new Date () return?

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.

What is new Date () in JavaScript?

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.


2 Answers

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

like image 63
Paul T. Avatar answered Oct 03 '22 10:10

Paul T.


Apparently because that (Unix timestamp 0) is what a Date object is initialized to by default, eh?

like image 22
bipll Avatar answered Oct 03 '22 11:10

bipll