if语句的条件应该等于true,但它不是

m.r*_*ice 0 javascript arrays

 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) });
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

上面的代码通过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;
                    }
                }
Run Code Online (Sandbox Code Playgroud)

当我安慰:

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

我明白了 -

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)
Run Code Online (Sandbox Code Playgroud)

那么为什么if语句不能评估为true?当我单步执行代码时,它会到达此行,然后步骤到下一个if.在此先感谢您的有益评论.

ade*_*neo 5

即使严格比较日期对象,两个对象也永远不会相同.

使用,比较epoch的毫秒数 getTime()

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