TheDome
TheDome

Reputation: 373

Why does new Date(year,month,day) not return an equivialent Date?

one day I fiddled with vanilla NodeJS using the node command line tool. (I am using node v13.11.0)

I tried to create a new Date at the 01.01.1970. I used the usual new Date(year, month, day) constructor. As simple as it sounds, I entered new Date(1970, 1, 1) and found out, that it does not return 1970-01-01T00:00:00.0000Z. Instead, it returns 1970-01-31T12:00:00.000Z.

Has anyone an Idea, why this constructor does not return the equivalent date?

Upvotes: 0

Views: 16

Answers (1)

Steve Bennett
Steve Bennett

Reputation: 126075

The constructor does more or less what you think:

x = new Date(1970,1,1)
1970-01-31T14:00:00.000Z
> x.getMonth()
1
> x.getDate()
1
> x.getHours()
0

(Note that months count from zero, so you requested the 1st of February).

But if you display the whole date as a string, it's showing the time in UTC, which might not be what you expect.

Upvotes: 1

Related Questions