Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do this dates not match?

i have this code which works with dates:

let startDate = new Date(toolOrder.jsAppStartDate.getTime()); // clone date
startDate.setDate(startDate.getDate() - Math.floor((days - 1) / 2)); // sub some days
console.log(toolOrder.jsAppStartDate); // Output: Date Thu Sep 21 2023 00:00:00 GMT+0200 (Central European Summer Time)
console.log(toolOrder.jsAppStartDate.getTime()); // Output: 1695247200000
console.log("--------------");
for (let index = 0; index < days; index++)
{
    console.log(startDate);
    console.log(startDate.getTime());
    // ... some drawing code here ...
    startDate.setDate(startDate.getDate() + 1); // add one day
    if(startDate === toolOrder.jsAppStartDate) // check if start date reached
    {
        console.log("Dates match");
        startDate = new Date(toolOrder.jsAppEndDate.getTime());
    }
}

The output of the log before the loop is:

Date Thu Sep 21 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695247200000
---------------

The output from the loop is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Date Mon Sep 18 2023 00:00:00 GMT+0200 (Central European Summer Time)
1694988000000 
Date Tue Sep 19 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695074400000
Date Wed Sep 20 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695160800000
Date Thu Sep 21 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695247200000
Date Fri Sep 22 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695333600000

So even if the dates match in loop 3, the if condition is not true. It works when I use

if(startDate.getTime() === toolOrder.jsAppStartDate.getTime())

but I thought it should work without also?

>Solution :

=== compares identity so:

startDate === toolOrder.jsAppStartDate

Means: is startDate the same object [in memory] as toolOrder.jsAppStartDate which is not the case because toolOrder.jsAppStartDate is a different object and not the object referred to by the name startDate.

mdn says this about the strict equality operator:

If both operands are objects, return true only if they refer to the same object.

(emphasis mine)

if(startDate.getTime() === toolOrder.jsAppStartDate.getTime())

Works because you’re now comparing numbers instead of objects.

The rule for the two operands being numbers is:

Otherwise, compare the two operand’s values:

Numbers must have the same numeric values. +0 and -0 are considered to be the same value.

mdn (emphasis mine)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading