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

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?

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!

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 ?

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;
1 Like

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

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;
1 Like

Will try these tweaks later today.
Thank you so much :+1:

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.

1 Like

Thanks @E1cid and @smcgann99 Job done!

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

[{"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 :grinning:

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