1

我需要计算表中的日期是否在 5 天内并更新父行的类。我能够做到这一点(在 FF 和 Chrome 中),但由于某种原因,它在 IE8 中不起作用。

表中的项目将具有类似“08/28/13”的日期,而 TD 将具有“时间”类。

$('td:contains("Scheduled")').parents('tr').addClass('scheduled');
$("td.time").each(function (index) {
    var date = $(this).text();
    //sep date
    var year = date.substring(6, 10);
    var month = date.substring(0, 2);
    var day = date.substring(3, 5);
    alert(month + ' ' + day + ', ' + year);
    //today
    var d = new Date();
    var today = d.getDate();

    var m = new Array();
    m[0] = "01";
    m[1] = "02";
    m[2] = "03";
    m[3] = "04";
    m[4] = "05";
    m[5] = "06";
    m[6] = "07";
    m[7] = "08";
    m[8] = "09";
    m[9] = "10";
    m[10] = "11";
    m[11] = "12";
    var b = d.getMonth();
    var n = parseInt(m[d.getMonth()]);

    var monthStart = new Date(year, b, 1);
    var monthEnd = new Date(year, b + 1, 1);
    var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);

    var nextMonth = n + 1;
    var dayDiff = 5 - (monthLength - today);
    if ((monthLength - 5) < today && nextMonth == month && day < dayDiff) {
        if (!$(this).parents('tr').hasClass('scheduled')) {
            $(this).parents('tr').addClass('urgent');
        };
    };
    if (n == month && today < day && day <= (today + 5)) {
        if (!$(this).parents('tr').hasClass('scheduled')) {
            $(this).parents('tr').addClass('urgent');
        };
    };
});
4

1 回答 1

1

我在@abc123 评论的问题中尝试了解决方案,并得到了 5 天之间基于数字时间戳的差异。我还更新了获得今天日期的方式。更少的代码。

$('td:contains("Scheduled")').parents('tr').addClass('scheduled');
$("td.time").each(function (index) {
    var date = $(this).text();
    var oppTime = date.substring(0, 10);

    var d = new Date();
    var today = d.toJSON();
    var t = today.substring(0, 4) + "/" + today.substring(5, 7) + "/" + today.substring(8, 10);

    var timeStamp_oppDate = new Date(oppTime).getTime();
    var timeStamp_thisDay = new Date(t).getTime();
    var timeDiff = timeStamp_oppDate - timeStamp_thisDay;

    if (432000000 >= timeDiff) {
        if (!$(this).parents('tr').hasClass('scheduled')) {
            $(this).parents('tr').addClass('urgent');
        };
    };
});
于 2013-08-27T14:29:50.637 回答