if 语句的条件应该等于 true,但事实并非如此

Condition of if statement should equal true but it doesn't

本文关键字:true 事实 并非如此 语句 条件 if      更新时间:2023-09-26
 if (emptyGrid) {
        ranges.push({ start: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].Begin), end: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].End) })
    }
    else {
        for (var i = 1; i < thisGridData.length; i++) {
            //debugger;                
            if (i === 1 && new Date(thisGridData[i].childEffBegDate) > new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].Begin)) {
                ranges.push({ start: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].Begin), end: new Date(moment(thisGridData[i].childEffBegDate).subtract(1, 'days').calendar()) });
            }
            else {
                if (i + 1 < thisGridData.length) {
                    //console.log('beginDate EndDate', new Date(thisGridData[i].childEffEndDate) + ' ' + new Date(thisGridData[i + 1].childEffBegDate));
                    console.log('in the and part', new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) + ' ' + new Date(thisGridData[i + 1].childEffBegDate));
                    //debugger;
                    if (new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) === new Date(thisGridData[i + 1].childEffBegDate)) {
                        if (i + 1 === thisGridData.length -1) {
                            return false;
                        }
                    }
                    else if (new Date(thisGridData[i].childEffEndDate) < new Date(thisGridData[i + 1].childEffBegDate) && new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) !== new Date(thisGridData[i + 1].childEffBegDate))
                        ranges.push({ start: new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()), end: new Date(moment(thisGridData[i + 1].childEffBegDate).subtract(1, 'days').calendar()) });
                }
                if (i === thisGridData.length - 1 && new Date(thisGridData[i].childEffEndDate) < new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].End))
                    ranges.push({ start: new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()), end: new Date(IndividualBugetFigures.awardYears_Unformatted[elementIndex].End) });
            }
        }
    }

上面的代码按应有的方式遍历 if 语句,除非它到达此部分:

if (new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) === new Date(thisGridData[i + 1].childEffBegDate)) {
                    if (i + 1 === thisGridData.length -1) {
                        return false;
                    }
                }

当我安慰时:

console.log('in the and part', new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) + ' ' + new Date(thisGridData[i + 1].childEffBegDate));

我得到——

in the and part Fri Jan 02 2015 00:00:00 GMT-0600 (Central Standard Time) Fri Jan 02 2015 00:00:00 GMT-0600 (Central Standard Time)

那么,为什么 if 语句的计算结果不为 true呢? 当我单步执行代码时,它会到达这一行,然后转到下一个 if 。 提前感谢您的有用评论。

两个对象永远不会相同,即使在严格比较日期对象时也是如此。

改为使用getTime()比较纪元中的毫秒数

if (new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()).getTime() === new Date(thisGridData[i + 1].childEffBegDate).getTime()) {...

您正在将日期实例与=== 进行比较。两个单独的 Date 实例,即使它们引用完全相同的时间点,也永远不会比较为相等,因为它们是对象。

您可以使用一元+操作数将 Date 实例强制为数字,并改为比较基础时间戳。

if (+new Date(moment(thisGridData[i].childEffEndDate).add(1, 'days').calendar()) === +new Date(thisGridData[i + 1].childEffBegDate)) {

此外,您应该能够以相同的方式使用 Momentjs 对象,并节省 Date 实例的构造。

您需要了解比较对象和某些特定值之间存在差异。 :)

这是我在谷歌搜索"比较日期值javascript"时发现的第一个结果(它是不同日期比较的摘要,如<,>,=,还有一个解释为什么它以前对你不起作用):

使用 JavaScript 比较两个日期