0

Code jsFiddle: http://jsfiddle.net/14rego/gEUJz/

I'm building an array of dates using these functions. Both work great in webkit browsers, but I know IE is pickier about dates.

nextDayA() works in IE as long as the months don't include August or September. I spent hours trying to figure it out before deciding to try nextDayB(), which I would think would work on any browser anywhere since it doesn't include the Date function, but it doesn't work at all in IE.

I don't care which one I use, as long as it works. I simply need to get the correct dates in order. I have a bit of experience, but I'm certainly not a guru. Can anyone shed some light?

$.nextDayA = function (year, month, day) {
    var jMo = parseInt(month) - 1;
    var today = new Date(year, jMo, day);
    var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
    var nextDy = tomorrow.getDate();
    var nextMo = tomorrow.getMonth() + 1;
    var nextYr = tomorrow.getFullYear();
    if (nextDy < 10) { nextDy = '0' + nextDy; }
    if (nextMo < 10) { nextMo = '0' + nextMo; }
    var tomDate = parseInt(nextYr + '' + nextMo + '' + nextDy);
    return tomDate;
};
$.nextDayB = function (year, month, day) {
    var thisYear = parseInt(year);
    var thisMonth = parseInt(month);
    var thisDay = parseInt(day);
    if (year % 4 == 0) {
        var leap = 'yes';
    } else {
        var leap = 'no';
    }
    var nextDy = thisDay + 1;
    if (nextDy > 28 && thisMonth === 2 && leap === 'no') {
        var nextDy = 1;
        var nextMo = thisMonth + 1;
    } else if (nextDy > 29 && thisMonth === 2 && leap === 'yes') {
        var nextDy = 1;
        var nextMo = thisMonth + 1;
    } else if (nextDy > 30 && (thisMonth === 2 || thisMonth === 4 || thisMonth === 6 || thisMonth === 9 || thisMonth === 11)) {
        var nextDy = 1;
        var nextMo = thisMonth + 1;
    } else if (nextDy > 31 && (thisMonth === 1 || thisMonth === 3 || thisMonth === 5 || thisMonth === 7 || thisMonth === 8 || thisMonth === 10 || thisMonth === 12)) {
        var nextDy = 1;
        var nextMo = thisMonth + 1;
    } else {
        var nextMo = thisMonth;
    }
    if (nextMo === 13) {
        var nextMo = 1;
        var nextYr = thisYear + 1;
    } else {
        var nextYr = thisYear;
    }

    if (nextDy < 10) {
        nextDy.toString();
        var strDy = '0' + nextDy;
    } else {
        var strDy = nextDy.toString();
    }
    if (nextMo < 10) {
        nextMo.toString();
        var strMo = '0' + nextMo;
    } else {
        var strMo = nextMo.toString();
    }

    var strYr = nextYr.toString();
    var strDate = strYr + '' + strMo + '' + strDy;
    var tomDate = parseInt(strDate);
    return tomDate;
};
regan_leah
  • 284
  • 2
  • 3
  • 14

1 Answers1

3

You have to pass a base parameter to parseInt():

var thisDay = parseInt(day, 10);

If you don't do that, then strings starting with "0" are interpreted as octal constants, not decimal. Of course, "08" and "09" are meaningless octal constants. The second parameter (10) tells the function that the string should be interpreted as a base 10 string regardless of leading zeros.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Interesting - looks like behaviour changed in IE9. Always wondered why JSLint was so fussy about always adding a radix to `parseInt()`. Now I know. Cheers :) – Tim Croydon Jan 16 '13 at 15:22
  • @TimCroydon I think that newer JavaScript specs (ES5?) have dropped support for octal, since it's pretty obviously not useful. – Pointy Jan 16 '13 at 15:23
  • yes, think you're right. Just been googling it - more detail here: http://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior – Tim Croydon Jan 16 '13 at 15:25
  • Wow, that was easy. Thank you so much. – regan_leah Jan 16 '13 at 15:31