javascript date creation, can't set the correct month

前端 未结 3 1917
天命终不由人
天命终不由人 2020-12-12 00:45
var d = new Date(2011,1,1);

alert(d);

this alert says February, while it should say January

anybody has some explanation for that ?

3条回答
  •  不思量自难忘°
    2020-12-12 01:11

    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.

提交回复
热议问题