表日期在Firefox中的颜色​​,但不在IE中

时间:2014-02-08 16:02:47

标签: javascript internet-explorer

此代码非常适用于过去已经过的颜色日期以及从现在开始的2周内的颜色日期。但只有在Firefox中,而不是在IE中,我可以调整代码,以便它也适用于IE吗?它与elem.style.color

的渲染有关

工作js fiddle

var parents = document.getElementsByTagName("tr")

for (var i = 0, ii = parents.length; i < ii; i++) {
    var parent = parents[i],
        children = parent.children

    for (var j = 0, jj = children.length; j < jj; j++) {
        var elem = children[j]
        if (j % 3 === 2) {
            var dateElement = elem.innerHTML;
            var dateArray = dateElement.split("-");
            var prevTime = new Date(dateArray);
            var thisTime = new Date();
            var diff = prevTime.getTime() - thisTime.getTime();
            var days = 1000*60*60*24;
            var diffInDays = Math.ceil(diff / days);
            if (diffInDays < 0) {
                elem.style.color = "#996600";
            }
            else if (diffInDays <= 14) {
                elem.style.color = "#e50005 ";
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于Internet Explorer的日期格式无效。

这是一个更新的Jsfiddle,也适用于IE。在IE中的Date构造函数中使用数组会导致无效日期。另请参阅此问题Passing an array to the Javascript Date constructor, is it standard?

var parents = document.getElementsByTagName("tr")

for (var i = 0, ii = parents.length; i < ii; i++) {
    var parent = parents[i],
        children = parent.children

    for (var j = 0, jj = children.length; j < jj; j++) {
        var elem = children[j]
        if (j % 3 === 2) {
            var dateElement = elem.innerHTML;
            var dateArray = dateElement.split("-");     
            var prevTime = new Date(dateArray[0],dateArray[1]-1,dateArray[2]);
            var thisTime = new Date();
            var diff = prevTime.getTime() - thisTime.getTime();
            var days = 1000*60*60*24;
            var diffInDays = Math.ceil(diff / days);
            if (diffInDays < 0) {
                elem.style.color = "#ffa500";
            }
            else if (diffInDays <= 14) {
                elem.style.color = "#ff0000";
            }
        }
    }
}