# Calculating days and hours between two timestamps - Almost there.. Need a tweak on this!

**URL:** <https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761>\
**Category:** General\
**Created:** [6 February 2023 00:11 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761 "2023-02-06T00:11:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [6 February 2023 00:11 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/1 "2023-02-06T00:11:55Z")

</div>

Hello!

So I'm calculating time between two payloads to display in a nice readable format in a function node.

**"X Days and X Hours"**

It's not quite working as anticipated.  
The read out of Days is incorrect. It calculates a whole Day after the 12th hour between timestamps. I'd like this to not happen till after the 24th hour.

_The time I am comparing is between now and the past... ie days and hours ago..._

Can anyone help fix this?

```auto
let current = new Date(msg.currentTime).valueOf();
let previous = new Date(msg.previousTime).valueOf();
let diff = current - previous;
let mins = Math.round((diff % 3600000) / 60000);
let hours = Math.floor(diff / 3600000)
let days = Math.round((current - previous) / (1000 * 60 * 60 * 24))
msg.calculatedInfo = days + " Days and " + hours % 24 + " Hours.";
return msg;

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 February 2023 02:04 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/2 "2023-02-06T02:04:49Z")

</div>

Change the order - so calculate days first then subtract the days from diff before doing hours, same again for mins, otherwise you will be counting them twice ?

```auto
let diff = current - previous/1000

var days = Math.floor(diff / 86400);
diff -= days * 86400;

var hours = Math.floor(diff / 3600) % 24;
diff -= hours * 3600;

var minutes = Math.floor(diff / 60) % 60;
diff -= minutes * 60;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 February 2023 07:52 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/3 "2023-02-06T07:52:02Z")

</div>

Error on first line, the division is done first, so parenthesis are required.  
Also the `diff -=` lines are not required.

```auto
let diff = (1675669264854 - 1675559264854) / 1000
var days = Math.floor(diff / 86400);
var hours = Math.floor(diff / 3600) % 24;
var minutes = Math.floor(diff / 60) % 60;

```

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [6 February 2023 08:11 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/4 "2023-02-06T08:11:46Z")

</div>

Will try these tweaks later today.  
Thank you so much 👍

---

<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:** [6 February 2023 08:20 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/5 "2023-02-06T08:20:35Z")

</div>

Note that no account is taken of DST change in the interval. So an interval from midnight local time immediately before a change, to midnight local time the next night would not give 1 day 0 hours. That may be exactly what you want.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [6 February 2023 16:28 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/6 "2023-02-06T16:28:28Z")

</div>

Thanks @E1cid and @Sean-McG Job done!

This is for anyone else who's looking for the same function, here's the flow I used.

```auto
[{"id":"60d94c9b8b7610c7","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"340903e4fe9fe902","type":"inject","z":"60d94c9b8b7610c7","name":"","props":[{"p":"currentTime","v":"","vt":"date"},{"p":"previousTime","v":"1675614000000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":240,"wires":[["d499865d483dfd64"]]},{"id":"d499865d483dfd64","type":"function","z":"60d94c9b8b7610c7","name":"function 2","func":"let current = new Date(msg.currentTime).valueOf();\nlet previous = new Date(msg.previousTime).valueOf();\nlet diff = (current - previous) / 1000\nvar days = Math.floor(diff / 86400);\nvar hours = Math.floor(diff / 3600) % 24;\nvar minutes = Math.floor(diff / 60) % 60;\nmsg.calculatedInfo = days + \" Days, \" + hours + \" Hours and \" + minutes + \" minutes\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":240,"wires":[["7240b26b27adf0e4"]]},{"id":"7240b26b27adf0e4","type":"debug","z":"60d94c9b8b7610c7","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":480,"y":240,"wires":[]}]

```

Thanks again for your valuable knowledge folks 😀

---

<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:** [20 February 2023 16:28 UTC](https://discourse.nodered.org/t/calculating-days-and-hours-between-two-timestamps-almost-there-need-a-tweak-on-this/74761/7 "2023-02-20T16:28:39Z")

</div>

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