Comparing times with the switch node

3,459 views
Skip to first unread message

Csongor Varga

unread,
Nov 15, 2016, 2:33:50 PM11/15/16
to Node-RED
Hi Guys,

Related to my last topic, I am trying to put together a flow which starts a process at a time (hour+minute) specified on the UI.

I use the below logic to store the start time (hour and minute) in global. "hour" and "minute" is coming from the UI.
    var mytime = new Date();
    mytime
.setHours(hour, minute, 0);
    mytime
.setMilliseconds(0);
    mytime
.setFullYear(2000,01,01);
   
global.set("start",mytime);

When I output this variable, I see this in the debug: Tue Feb 01 2000 20:22:00 GMT+0100 (CET). This looks OK for me.

Next, I create a inject to execute every second and send out the timestamp. In a function connected to the inject, I convert the message like this:
var current = new Date();
current
.setFullYear(2000,01,01);
current
.setMilliseconds(0);
msg
.payload = current;

In the next step I compare the msg.payload == global.start in a switch node. And it never comes true.

I added the payload to debug and it is definitely firing every second. I can see that it picks up the same value as in the global variable in the debug. In both cases I have setMilliseconds(0) to make sure the sub-second values are nil for both. And it is still not happening. 
If I am using operators other than == it works just fine. So I am sure switch can compare dates.

Any ideas where I have gone wrong?

Dave C-J

unread,
Nov 15, 2016, 2:52:06 PM11/15/16
to node...@googlegroups.com

Csongor Varga

unread,
Nov 15, 2016, 3:18:27 PM11/15/16
to Node-RED
Thanks you very much, I did not know this. I changed the logic, so it is converting the dates to millis using getTime() function. And it works now.
Reply all
Reply to author
Forward
0 new messages