# Time Calculation

**URL:** <https://discourse.nodered.org/t/time-calculation/74980>\
**Category:** General\
**Created:** [10 February 2023 07:25 UTC](https://discourse.nodered.org/t/time-calculation/74980 "2023-02-10T07:25:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grant.cpt](https://avatars.discourse-cdn.com/v4/letter/g/eb8c5e/32.png) [@grant.cpt](https://discourse.nodered.org/u/grant.cpt)\
**Post date:** [10 February 2023 07:25 UTC](https://discourse.nodered.org/t/time-calculation/74980/1 "2023-02-10T07:25:14Z")

</div>

Hi All,

I've got a use case I want to try and solve. And that's calculating the time between for a value to change from x to y.

Starting Temperature: 50  
Time: 10:00:00  
Next value: 51  
Time: 10:05:00

So the output will be 5 as that's the time that passed for the value to change from 50 to 51.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 February 2023 13:08 UTC](https://discourse.nodered.org/t/time-calculation/74980/2 "2023-02-10T13:08:54Z")

</div>

Here is a function to calculate the time difference between successive message timestamps.  
If the message sent at the trigger temperature does not contain a timestamp, you would have to generate one in the function.  
 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/94710729047aa0693d51af586b016c657934da27.jpeg)

```auto
[{"id":"9648da80c2c77166","type":"inject","z":"ac3483be8df714b3","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":120,"wires":[["28c953b77c5bcf93"]]},{"id":"28c953b77c5bcf93","type":"function","z":"ac3483be8df714b3","name":"function 1","func":"function timeDifference(ts1, ts2) {\n var millisec = ts1 - ts2;\n const days = Math.floor(millisec / 1000 / 60 / 60 / 24);\n millisec -= days * 1000 * 60 * 60 * 24\n const hrs = Math.floor(millisec / 1000 / 60 / 60);\n millisec -= hrs * 1000 * 60 * 60\n const mins = Math.floor(millisec / 1000 / 60);\n millisec -= mins * 1000 * 60\n const secs = Math.floor(millisec / 1000);\n millisec -= secs * 1000\n return { \"days\": days, \"hrs\": hrs, \"mins\": mins, \"secs\": secs, \"ms\": millisec }\n}\n\nconst previous = flow.get(\"lasttimestamp\") ?? 0\n\nif (previous == 0) { \n flow.set(\"lasttimestamp\", msg.payload) \n msg.payload = \"Waiting for second timestamp\"\n}\nelse {\n flow.set(\"lasttimestamp\", msg.payload)\n msg.payload = timeDifference(msg.payload, previous)\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":120,"wires":[["a0d70c27a8570170"]]},{"id":"a0d70c27a8570170","type":"debug","z":"ac3483be8df714b3","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":120,"wires":[]}]

```

```auto
{"days":0,"hrs":0,"mins":2,"secs":55,"ms":575}

```

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [10 February 2023 20:21 UTC](https://discourse.nodered.org/t/time-calculation/74980/3 "2023-02-10T20:21:16Z")

</div>

An alternative to jbudd's offering. Only goes down to seconds (uses same format as Tasmota)

```auto
[{"id":"d6896368821fb2e4","type":"function","z":"34ad08af41ba9a25","name":"function 5","func":" /**\n * Description Converts milliseconds to days T hours:minutes:seconds string in 'locale' format. (Format used by Tasmota)\n * Create a Date object with the number of milliseconds, rounding the number to the nearest second\n * Get the number of days from the number of milliseconds\n * Convert Date to time string using UTC as the time zone to avoid daylight saving errors (or time zone offset)\n *\n * @param {number} milliSeconds - Number of milliseconds to convert. Usually as a response to Date.now() or Date.getTime()\n *\n * @returns {string} - Timespan string. In UK \"ddThh:mm:ss\"\n *\n */\n\n function msToTimespan(milliSeconds) {\n if (milliSeconds !== 0) {\n const date = new Date(milliSeconds)\n const days = Math.floor(milliSeconds / (3600000 * 24))\n\n return `${days}T${date.toLocaleTimeString('en-GB', { timeZone: 'UTC' })}`\n\n }\n\n return '0T00:00:00'\n\n } // End Method msToTimespan()\n\nconst lastTime = context.get('lastTime') ?? 0\nconst timeStamp = msg.payload\n\nif (lastTime > 0) {\n msg.payload = msToTimespan(timeStamp - lastTime)\n\n} else {\n msg.payload = 0\n\n}\n\ncontext.set('lastTime', timeStamp)\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1760,"y":1600,"wires":[["339242f4565ed930"]]},{"id":"339242f4565ed930","type":"debug","z":"34ad08af41ba9a25","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2020,"y":1600,"wires":[]},{"id":"e9c08a738775d06e","type":"inject","z":"34ad08af41ba9a25","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1480,"y":1600,"wires":[["d6896368821fb2e4"]]}]

```

0T00:01:51

---

<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:** [11 April 2023 20:21 UTC](https://discourse.nodered.org/t/time-calculation/74980/4 "2023-04-11T20:21:53Z")

</div>

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