javascript date creation, can't set the correct month

前端 未结 3 1911
天命终不由人
天命终不由人 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.

    0 讨论(0)
  • 2020-12-12 01:21

    JavaScripts Date object zero-indexes months.

    Try:

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

    Instead.

    See the documentation for more info!

    0 讨论(0)
  • 2020-12-12 01:23

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

    var d = new Date(2011, 0, 1);
    
    0 讨论(0)
提交回复
热议问题