如何正确比较MM-dd-yyyy字符串中的年份

How to correctly compare years within MM-dd-yyyy strings?

本文关键字:字符串 何正确 比较 MM-dd-yyyy      更新时间:2023-09-26

我使用date.js对数据网格应用条件格式。数据是从javascript数组中解析的。我所有的条件都正常工作,除了这个:

if (val <今天,,Val> '01-01-2000')

val是一个不能更改的MM-dd-yyyy格式的字符串。所以我使用date.js将今天的日期转换为MM-dd-yyyy格式的字符串并进行比较。问题是01-17-2014被视为小于04-08-2013 -因为它比较的是字符串。

解决这个问题的最好方法是什么?

我想使它简单,这就是为什么我首先转换为字符串,但我不确定如何解决年份问题。

谢谢你的帮助!

var today = new Date.today().toString("MM-dd-yyyy");
var tomorrow = new Date.today().addDays(1).toString("MM-dd-yyyy");
var upcoming = new Date.today().addDays(7).toString("MM-dd-yyyy");
function eXcell_edncl(cell) {
    this.base = eXcell_edn;
    this.base(cell);
    this.setValue = function(val) {
        if (val.indexOf('ACT') >= 0) this.cell.style.backgroundColor="lightgreen";
        else if (val.indexOf('PV') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px";
        else if (val.indexOf('YES') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px";
        else if (val < today && val > '01-01-2000') this.cell.style.backgroundColor="red";
        else if (val == today) this.cell.style.backgroundColor="orange";
        else if (val == tomorrow) this.cell.style.backgroundColor="yellow";
        else if (val > tomorrow && val <= upcoming) this.cell.style.backgroundColor="lightyellow";
        else this.cell.style.backgroundColor="";
        this.cell.innerHTML = this.grid._aplNF(val, this.cell._cellIndex);
    }
}

解决这个问题的最好方法是不要将Date对象转换为string。"01-17-2014" < "04-08-2013"的计算结果为真,因为"01" < "04"是真,所以无论附加到这些字符串上的是什么,计算结果总是一样的。但是,在Date对象上使用小于/大于操作符将符合预期。所以你可以修改现有的if语句为

if (new Date(val) < new Date(today) && new Date(val) > new Date('01-01-2000'))

,这将解决您的问题,但您可能最好开始使用Date对象。

既然你使用的是date.js,你可以使用它的比较函数,如文档中所述:

Date.compare (Date date1, Date date2): Number

比较第一个日期和第二个日期,并返回一个数字表示它们的相对值。-1 =小于日期。0 =值相等。1 = this is greaterthan date.

您可能无法在UI中更改日期字符串的格式,但在后端使用的格式应该无关紧要。您应该更改代码以使用ISO 8601,它的设计允许在字符串格式中轻松比较(以及其他优点)。

日期的格式将是yyyy-MM-dd,这将允许您将它们作为字符串进行比较。

因为它是相关的,如果你还在观望,看看这个XKCD漫画。

以以下工作结束。需要解析传入的值:

var today = new Date.today().toString("MM-dd-yyyy");
var today2 = new Date.today();
var old = new Date(2000, 0, 1);
var tomorrow = new Date.today().addDays(1).toString("MM-dd-yyyy");
var upcoming = new Date.today().addDays(7).toString("MM-dd-yyyy");
function eXcell_edncl(cell) {
    this.base = eXcell_edn;
    this.base(cell);
    this.setValue = function(val) {
        var val2 = new Date.parse(val);
        if (val.indexOf('ACT') >= 0) this.cell.style.backgroundColor="lightgreen";
        else if (val.indexOf('PV') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px";
        else if (val.indexOf('YES') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px";
        else if (val2 < today2 && val2 > old) this.cell.style.backgroundColor="red";
        else if (val == today) this.cell.style.backgroundColor="orange";
        else if (val == tomorrow) this.cell.style.backgroundColor="yellow";
        else if (val > tomorrow && val <= upcoming) this.cell.style.backgroundColor="lightyellow";
        else this.cell.style.backgroundColor="";
        this.cell.innerHTML = this.grid._aplNF(val, this.cell._cellIndex);
    }
}