How to calculate future time

I have a manual target temperature calculator, where operator enters minute as number. I would like to calculate future time (yellow highlighted) by adding minutes (manual entry on left) to the current time.

The moment node should be able to do that for you I think.

Otherwise, you will need to look up how to do it in JavaScript and use a function node.

Since no one jumped in yet I'll give my two cents. Time is a funny animal to play with but I'd convert the current time to epoch time, add the minutes then convert back. There are probably better ways but at least this would work.

Put his in a function node and place the function node between the nodes.

msg.payload=Date.now() + (msg.payload*1000*60) //1000 milisec in a second, 60 seconds in a minute
return msg;

@PinPat Are you using node-RED v1.3 or above?
If so, you could use the day.js package in a function node to both calculate the new time, and also format it to however you wish.
Day.js is a very lightweight package (2kB) and full docs are here.

Example Flow for node-RED v1.3 or above only - This will load the day.js package into node-RED's externalModules folder

dayjs

[{"id":"94c3a6b3.922108","type":"function","z":"b3b413d1.05b1b","name":"","func":"let now=dayjs().add(msg.payload, 'minutes');\nmsg.payload=(now.format(\"HH:mm:ss\"));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dayjs","module":"dayjs"}],"x":345,"y":1380,"wires":[["79269a6e.fa51e4"]]},{"id":"dadca7a6.ebc708","type":"inject","z":"b3b413d1.05b1b","name":"Add 30 mins","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":170,"y":1380,"wires":[["94c3a6b3.922108"]]},{"id":"79269a6e.fa51e4","type":"debug","z":"b3b413d1.05b1b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":500,"y":1380,"wires":[]},{"id":"e84043ba.b05da","type":"inject","z":"b3b413d1.05b1b","name":"Add 45 mins","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"45","payloadType":"num","x":170,"y":1420,"wires":[["94c3a6b3.922108"]]},{"id":"a470cd92.fd24c","type":"inject","z":"b3b413d1.05b1b","name":"Add 10 mins","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":170,"y":1340,"wires":[["94c3a6b3.922108"]]}]
1 Like


Finally i got epoch time. How do i convert it to HH:mm:ss or hh:mm:ss AM/PM ?

Thanks . Moment node worked fine for me.

1 Like

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