Date of change of results, if called with an array of parameters

I tried the following script with both Node.js and my Chrome console:

console.log(new Date([1988,11,5]))
console.log(new Date(1988,11,5))

I expected the two methods to give the same results, but they behave differently:

Sat Nov 05 1988 00:00:00 GMT+0100 (W. Europe Standard Time)
Mon Dec 05 1988 00:00:00 GMT+0100 (W. Europe Standard Time)

In particular, the second field that defines the month starts counting from zero in the first case.

Would I expect a month starting from zero in both cases, or did I not understand something?

+4
source share
2 answers

The second call new Date(1988, 11, 5)corresponds to the specific format required by the specification for the constructor Date:

new Date (year, month [, date [, hours [, minutes [, seconds [, ms]]]]])

, :

()

, , ToPrimitive, . ToPrimitive [[Default Value]], , value, new Date , , 1988,11,5.

spec new Date (value), , - String, , , Date.Parse, :

, (15.9.1.15). , . , String, . , NaN.

( ).

, JavaScript V8, Chrome Node.js ' , , 1. , , /, .

+1

Date; . , yyyy,mm (1 based),dd.

: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

init:

console.log(new Date([1988,11,5]))
console.log(new Date([1988,11,5].toString()))
console.log(new Date("1988,11,5"))

: https://jsfiddle.net/IrvinDominin/Lweww6et/

+2

Source: https://habr.com/ru/post/1622077/


All Articles