0

代码 jsFiddle:http: //jsfiddle.net/14rego/gEUJz/

我正在使用这些函数构建日期数组。两者都在 webkit 浏览器中运行良好,但我知道 IE 对日期更挑剔。

只要月份不包括八月或九月,nextDayA() 就可以在 IE 中使用。在决定尝试 nextDayB() 之前,我花了几个小时试图弄清楚它,我认为它可以在任何地方的任何浏览器上工作,因为它不包含 Date 函数,但它在 IE 中根本不起作用。

我不在乎我使用哪一个,只要它有效。我只需要按顺序获得正确的日期。我有一点经验,但我肯定不是大师。任何人都可以解释一下吗?

$.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;
};
4

1 回答 1

3

您必须将基本参数传递给parseInt()

var thisDay = parseInt(day, 10);

如果您不这样做,则以“0”开头的字符串将被解释为八进制常量,而不是十进制。当然,“08”和“09”是无意义的八进制常数。第二个参数 (10) 告诉函数该字符串应被解释为以 10 为底的字符串,而不管前导零如何。

于 2013-01-16T15:18:48.783 回答