# How to calculate time difference?

**URL:** https://discourse.nodered.org/t/how-to-calculate-time-difference/7857
**Category:** General
**Created:** [12 February 2019 03:37 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference/7857 "2019-02-12T03:37:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![empirejrz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/empirejrz/32/9533_2.png) [@empirejrz](https://discourse.nodered.org/u/empirejrz)
#### Post date: [12 February 2019 03:37 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference/7857/1 "2019-02-12T03:37:41Z")

</div>

Hello;

```
I would like to calculate time when start 22:00:00 in the database and the end time is 02:30:00. Who has an idea for calculate 22:00:00 to 02:30:00( Total 04:30:00 Hour ) in the node-red? 

```

Thank you.

---

<div class="post-metadata">

### Author: ![EMPadvocate](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/empadvocate/32/22338_2.png) [@EMPadvocate](https://discourse.nodered.org/u/EMPadvocate)
#### Post date: [12 February 2019 04:38 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference/7857/2 "2019-02-12T04:38:47Z")

</div>

Here is code I have used to figure sunset and sunrise .. perhaps this could help.  
hr=msg.payload.sun\_phase.sunrise.hour;  
min=msg.payload.sun\_phase.sunrise.minute;  
srise = hr+':'+min;  
sriseCalc = new Date();  
sriseCalc = execTime(sriseCalc, srise, -15);  
hr=msg.payload.sun\_phase.sunset.hour;  
min=msg.payload.sun\_phase.sunset.minute;  
sset = hr+':'+min;  
ssetCalc = new Date();  
ssetCalc = execTime(ssetCalc, sset, 15);  
msg.payload = {sunrise : srise, sunset : sset,calcSunrise : sriseCalc.getTime(), calcSunset : ssetCalc.getTime()};  
context.global.astroData = {calcSunrise : sriseCalc.getTime(), calcSunset : ssetCalc.getTime()};// context.global.astroData;  
return msg;

function execTime (t, target, delay) {  
arr = target.split(':');  
t.setHours(arr[0]);  
t.setMinutes(arr[1]);  
t.setMinutes(t.getMinutes() + (delay));  
return t;  
}

---

<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 February 2019 07:09 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference/7857/3 "2019-02-12T07:09:09Z")

</div>

As always, don't forget to think about local vs UTC times. If using local, you need to take into account any daylight savings changes.

---

<div class="post-metadata">

### Author: ![empirejrz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/empirejrz/32/9533_2.png) [@empirejrz](https://discourse.nodered.org/u/empirejrz)
#### Post date: [12 February 2019 08:19 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference/7857/4 "2019-02-12T08:19:23Z")

</div>

Thank you very much. I will do it.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 February 2019 09:59 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference/7857/5 "2019-02-12T09:59:59Z")

</div>

Assuming the times are javascript Date objects then you can just subtract them to get the difference in milliseconds. Simple example:

```auto
[{"id":"6ad54371.3f495c","type":"inject","z":"514a90a5.c7bae8","name":"Go","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":86.5,"y":386,"wires":[["aefc190e.4d5228"]]},{"id":"aefc190e.4d5228","type":"function","z":"514a90a5.c7bae8","name":"Set start time","func":"var now = new Date(); // current time\nflow.set(\"startTime\", now);\nreturn msg;","outputs":1,"noerr":0,"x":242.5,"y":386,"wires":[["7a2a486a.8ed5e8"]]},{"id":"9ec23600.57f388","type":"function","z":"514a90a5.c7bae8","name":"Calc difference ms","func":"var now = new Date();\nmsg.payload = now - flow.get(\"startTime\");\nreturn msg;","outputs":1,"noerr":0,"x":579.5,"y":386,"wires":[["ddbc6e32.6a8f28"]]},{"id":"7a2a486a.8ed5e8","type":"delay","z":"514a90a5.c7bae8","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":408.5,"y":386,"wires":[["9ec23600.57f388"]]},{"id":"ddbc6e32.6a8f28","type":"debug","z":"514a90a5.c7bae8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":522.5,"y":495,"wires":[]}]

```
