Date problem with now === stopTime

Why now.getTime === stopTime.getTime() works,
but not now === stopTime?

[{"id":"9d15a9187c914833","type":"tab","label":"Flow 12","disabled":false,"info":"","env":[]},{"id":"c43a8169fed4b72e","type":"junction","z":"9d15a9187c914833","x":300,"y":180,"wires":[["29eb42608a865bce"]]},{"id":"29eb42608a865bce","type":"function","z":"9d15a9187c914833","name":"function 60 ","func":"const now = new Date();\nnow.setSeconds(0)\nnow.setMilliseconds(0)\n\n// Set start a copple of minutes ahead before deploy\nlet start = 55\nlet stop = start + 2\n\nconst startTime = new Date(now)\nconst stopTime = new Date(now)\nstartTime.setMinutes(start)\nstopTime.setMinutes(stop)\n\nnode.warn(now.getTime())\n\nif (now >= startTime && now <= stopTime) {\n\n    if (now < stopTime) {\n        node.warn('now < stoptime');\n    }\n    \n    node.warn('0 ' + now)\n    node.warn('1 ' + startTime)\n    node.warn('2 ' + stopTime)\n\n    //Why does this not work?\n    if(now === stopTime) {\n        node.warn('now === stopTime')\n    }\n\n    // But this does\n    if (now.getTime() === stopTime.getTime()) {\n        node.warn('now.getTime() === stopTime.genTime')\n    }\n}\n\n\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":180,"wires":[["51a892843386397a"]]},{"id":"51a892843386397a","type":"debug","z":"9d15a9187c914833","name":"debug 2593","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":179,"wires":[]},{"id":"394dedca39d49512","type":"inject","z":"9d15a9187c914833","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":179,"wires":[["c43a8169fed4b72e"]]}]
const now = new Date();
now.setSeconds(0)
now.setMilliseconds(0)

// Set start a copple of minutes ahead before deploy
let start = 55
let stop = start + 2

const startTime = new Date(now)
const stopTime = new Date(now)
startTime.setMinutes(start)
stopTime.setMinutes(stop)

node.warn(now.getTime())

if (now >= startTime && now <= stopTime) {

    if (now < stopTime) {
        node.warn('now < stoptime');
    }
    
    node.warn('0 ' + now)
    node.warn('1 ' + startTime)
    node.warn('2 ' + stopTime)

    //Why does this not work?
    if(now === stopTime) {
        node.warn('now === stopTime')
    }

    // But this does
    if (now.getTime() === stopTime.getTime()) {
        node.warn('now.getTime() === stopTime.genTime')
    }
}




because now is a date object. Objects are references to a "thing" in memory. Since you have 2 "things" in memory, they have different pointers to a space in memory and they will NEVER equal :slight_smile:

To compare date objects use date1.valueOf() === date2.valueOf()

now.getTime() and stopTime.getTime() are integer values, which can be compared using ===.

now and stopTime are Date objects. If you use to === to compare objects you are asking if they are the same object, which they are not. They contain the same time value but are not the same object in memory. I don't know whether it would work if you used ==, but personally I would use getTime() in order to make it clear what you are doing.

Funnily enough, <, <= and >= work, so that's why I thought == or === worked too.
Whether I use now or aaaa makes no difference.

Have you tried ==?

Yes, I seem to have tried everything, but who knows.
I'm on mac m1 with latest update, and
18 Sep 20:57:21 - [info] Node-RED version: v4.0.3
Sep 18 20:57:21 - [info] Node.js version: v21.7.1
Sep 18 20:57:21 - [info] Darwin 24.0.0 arm64 LE
When I get home I'll try an rpi.

Having done some testing I see you are right. With two Date objects, < and > appear to work as expected but == always fails. I cannot explain that.

Does > and < force them to be numeric for comparison purposes ?

It would appear so, but then, probably naively, I had assumed that == would do the same.

using the < and > causes the default valueOf to be called. Easy enough to prove if you override the prototype to console log something.

I wonder what is the logic of == not doing that.

Not so easy for those of us who don't know how to do that, and without the experience to even think of it. We learn every day, fortunately. Though remembering is a different matter :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.