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

后端 未结 5 454
鱼传尺愫
鱼传尺愫 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:01

    The problem is that today is July 31.

    When you set:

    var date = new Date();
    

    Then date.getUTCDate() is 31. When you set date.setUTCMonth(5) (for June), you are setting date to June 31. Because there is no June 31, the JavaScript Date object turns it into July 1. So immediately after setting calling date.setUTCMonth(5) if you alert(date.getUTCMonth()); it will be 6.

    This isn't unique to June. Using your function on the 31st of any month for any other month that does not have 31 days will exhibit the same problem. Using your function on the 29th (non-leap years), 30th or 31st of any month for February would also return the wrong result.

    Calling setUTC*() in such a way that any rollovers are overwritten by the correct value should fix this:

    var date = new Date();
    date.setUTCMilliseconds(0);
    date.setUTCSeconds(parseInt(matches[6], 10));
    date.setUTCMinutes(parseInt(matches[5], 10) - offset);
    date.setUTCHours(parseInt(matches[4], 10));
    date.setUTCDate(parseInt(matches[3], 10));
    date.setUTCMonth(parseInt(matches[2], 10) - 1);
    date.setUTCFullYear(parseInt(matches[1], 10));
    
    0 讨论(0)
  • 2020-12-18 05:10

    Looks like a bug?

    C:\Documents and Settings\me>java org.mozilla.javascript.tools.shell.Main
    Rhino 1.7 release 2 2009 03 22
    js> date = new Date();
    Fri Jul 31 2009 15:18:38 GMT-0400 (EDT)
    js> date.setUTCMonth(5); date.toUTCString();
    Wed, 01 Jul 2009 19:18:38 GMT
    js> date.setUTCMonth(5); date.toUTCString();
    Mon, 01 Jun 2009 19:18:38 GMT
    

    EDIT: Nevermind I guess. Question already answered by somebody more knowledgable.

    0 讨论(0)
  • 2020-12-18 05:12

    It's the order in which you are changing the date. The date starts out as July 31, so the set of the month fails because there is no 31 in June. (Actually it is rolling over to jul-1.)

    After setting the full year, add this:

    date.setYUTCDate(1);
    

    It makes it the first of the month wherein every month is valid.

    0 讨论(0)
  • 2020-12-18 05:17

    It's because today is July 31. Grant explained the problem. Here's what I believe is a simpler solution. Initialize your date on Jan 1.

    var date = new Date(2009,0,1,0,0,0);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题