Thomas
Thomas

Reputation: 577

new Date() function not working correctly

Does anyone has any idea why

new Date('2012','1','1')

returns 1 february 2012 instead of 1 january?

and

new Date('2012','0','1')

will return 1 January 2012

Is this weird or logical?

Upvotes: 0

Views: 1033

Answers (2)

Ngoan Tran
Ngoan Tran

Reputation: 1527

Please take some time to read the document of JavaScript Dates below: https://www.w3schools.com/js/js_dates.asp

It has noticed that :

JavaScript counts months from 0 to 11. January is 0. December is 11.

So the result you got is absolutely correct.

Upvotes: 1

aecolley
aecolley

Reputation: 2011

Because that's exactly what section 15.9.1.4 of the ECMAscript specification (3rd edition) says should happen. 0=Jan, 1=Feb, ... 11=Dec.

http://www.ecma-international.org/publications/standards/Ecma-262.htm

Upvotes: 6

Related Questions