# How to calculate future time

**URL:** https://discourse.nodered.org/t/how-to-calculate-future-time/44035
**Category:** Dashboard
**Created:** [12 April 2021 13:57 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035 "2021-04-12T13:57:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![PinPat](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@PinPat](https://discourse.nodered.org/u/PinPat)
#### Post date: [12 April 2021 13:57 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/1 "2021-04-12T13:57:42Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc5881c02855be5d398453c8f01ccc6db3fad7e4.png)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [12 April 2021 17:18 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/2 "2021-04-12T17:18:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [12 April 2021 17:20 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/3 "2021-04-12T17:20:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [12 April 2021 18:00 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/4 "2021-04-12T18:00:29Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [12 April 2021 19:10 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/5 "2021-04-12T19:10:59Z")

</div>

@PinPat Are you using node-RED v1.3 or above?  
If so, you could use the [day.js package](https://www.npmjs.com/package/dayjs) 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](https://day.js.org/docs/en/installation/installation).

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

![dayjs](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4b7c286f5871a63804901a58af6956c92e50ed4f.jpeg)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

### Author: ![PinPat](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@PinPat](https://discourse.nodered.org/u/PinPat)
#### Post date: [13 April 2021 04:05 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/6 "2021-04-13T04:05:35Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac795c6735e35124f4ccde9c49b87147ff15772f.png)  
Finally i got epoch time. How do i convert it to HH:mm:ss or hh:mm:ss AM/PM ?

---

<div class="post-metadata">

### Author: ![PinPat](https://avatars.discourse-cdn.com/v4/letter/p/ad7895/32.png) [@PinPat](https://discourse.nodered.org/u/PinPat)
#### Post date: [13 April 2021 06:26 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/7 "2021-04-13T06:26:50Z")

</div>

Thanks . Moment node worked fine for me.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [13 May 2021 06:27 UTC](https://discourse.nodered.org/t/how-to-calculate-future-time/44035/8 "2021-05-13T06:27:18Z")

</div>

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