开发者

javascript date creation, can't set the correct month

开发者 https://www.devze.com 2023-03-25 13:20 出处:网络
var d = new Date(2011,1,1); alert(d); this alert says 开发者_StackOverflow中文版February, while it should say January
var d = new Date(2011,1,1);

alert(d);

this alert says 开发者_StackOverflow中文版February, while it should say January

anybody has some explanation for that ?


JavaScripts Date object zero-indexes months.

Try:

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

alert(d);

Instead.

See the documentation for more info!


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号