Javascript date parsing bug - fails for dates in June (??)

后端 未结 5 453
鱼传尺愫
鱼传尺愫 2020-12-18 04:28

I have some javascript which parses an ISO-8601 date. For some reason, it is failing for dates in June. But dates in July and May work fine, which doesn\'t make sense to m

5条回答
  •  有刺的猬
    2020-12-18 05:20

    The date object starts off with the current date. It's the 31st today so setting 2009-06-09 gives:

    var date = new Date();     // Date is 2009-07-31
    date.setUTCFullYear(2009); // Date is 2009-07-31
    date.setUTCMonth(6 - 1);   // Date is 2009-06-31 = 2009-07-01
    date.setUTCDate(9);        // Date is 2009-07-09
    

    If you set the date to the 1st before you begin, then you should be safe.

提交回复
热议问题