简体   繁体   中英

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

 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) });
            }
        }
    }

The above code goes through the if statements as it should except when it gets to this part:

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;
                    }
                }

When I console out:

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

I get --

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)

So why doesn't the if statement evaluate to true? When I step through the code it gets to this line and then steps to next else if. Thanks in advance for the helpful comments.

Two objects are never the same, even when strictly comparing date objects.

Compare the milliseconds from epoch instead, using getTime()

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

You're comparing Date instances with === . Two separate Date instances, even when they refer to the exact same point in time, will never compare as being equal, because they're objects.

You can coerce the Date instances to numbers with the unary + operand and compare the underlying timestamps instead.

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

Also, you should be able to use a Momentjs object the same way, and save on the construction of a Date instance.

You need to understand that there is a difference between comparing objects and some specific values. :)

Here is the first result I have found when googling for "compare date values javascript" (it's a summary of different date comparisons like <, >, =, and there is also an explanation why it did not work for you before):

Compare two dates with JavaScript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM