Omu
Omu

Reputation: 71288

javascript date creation, can't set the correct month

var d = new Date(2011,1,1);

alert(d);

this alert says February, while it should say January

anybody has some explanation for that ?

Upvotes: 2

Views: 245

Answers (3)

ShankarSangoli
ShankarSangoli

Reputation: 69915

The month argument is 0 based so you should pass 0 for January.

var d = new Date(2011, 0, 1);

Upvotes: 3

hvgotcodes
hvgotcodes

Reputation: 120308

the month argument is zero based. So 0 = jan, 1 = feb, etc....

Look here. Specifically at the part that says

The setMonth() method sets the month (from 0 to 11), according to local time.

Note: January is 0, February is 1, and so on.

Upvotes: 6

Matt
Matt

Reputation: 75327

JavaScripts Date object zero-indexes months.

Try:

var d = new Date(2011,0,1);

alert(d);

Instead.

See the documentation for more info!

Upvotes: 8

Related Questions