# Time subtraction in a function node

**URL:** <https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774>\
**Category:** General\
**Created:** [30 November 2020 20:31 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774 "2020-11-30T20:31:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RASelkirk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raselkirk/32/57425_2.png) [@RASelkirk](https://discourse.nodered.org/u/RASelkirk)\
**Post date:** [30 November 2020 20:31 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/1 "2020-11-30T20:31:48Z")

</div>

Hi All,

I'm not a programmer by any means. I'm trying to figure out how to measure the seconds/milliseconds between two different node events which should end up around 20.xxx seconds. I'm experimenting with a simpletime node as a trigger, mysecond/my millis checked, then passed to the function. I have divided the seconds by 1 to force it to a number, and the millis by 1000 to get a decimal (and force a number). While watching the debug node, it occurred to me that the seconds wrap over after 59. Now I'm stuck handling this phenomenon. Would I be better off with a simple timestamp?

Russ

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 November 2020 20:37 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/2 "2020-11-30T20:37:39Z")

</div>

> [@RASelkirk](#):
>
> Would I be better off with a simple timestamp?

Yes. No need for any contrib nodes.

```auto
var ts1 = msg.payload; // some other time
var diffMS = ts1 - Date.now();
var diffS = diffMS / 1000;
msg.payload = diffS;
return msg;

```

---

<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:** [30 November 2020 22:14 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/3 "2020-11-30T22:14:43Z")

</div>

Alternatively if you look on the Flows site you will find at least one node that measures the times between messages sent to it, so if you send it a start and stop message it will tell you the time between.

---

<div class="post-metadata">

**Author:** ![RASelkirk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/raselkirk/32/57425_2.png) [@RASelkirk](https://discourse.nodered.org/u/RASelkirk)\
**Post date:** [1 December 2020 22:50 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/4 "2020-12-01T22:50:43Z")

</div>

Colin,

I actually found that flow by accident yesterday morning, imported it & piddled with it, then moved on. After your mention, I went back and tried to find it with no luck. In the meantime, I found Bart B's "Interval" node which works just like I'd hoped. I really wanted to accomplish something on my own, but I'm not quite ready yet 😩

Oh, Steve Mcl, I tried your code and it threw an "NaN". I've seen that before on some of my number puzzle sites, what does that mean?

Russ

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 December 2020 22:59 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/5 "2020-12-01T22:59:34Z")

</div>

> [@RASelkirk](#):
>
> Oh, Steve Mcl, I tried your code and it threw an "NaN". I've seen that before on some of my number puzzle sites, what does that mean?

NaN = Not a Number

it probably means means `msg.payload` didnt have a timestamp in it

![LIeUPDt7M8](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f0db88f38ff89af1dab6e7b661f3098f499367a.gif)

This is easy to confirm be using a debug node.

PS, the code i posted does work. Demo...

```auto
[{"id":"e34dd9b5.008118","type":"debug","z":"b83d9b41.9c80c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1290,"y":240,"wires":[]},{"id":"7abe0182.47052","type":"inject","z":"b83d9b41.9c80c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1140,"y":180,"wires":[["d6daa69e.7b89e8"]]},{"id":"d6daa69e.7b89e8","type":"function","z":"b83d9b41.9c80c8","name":"","func":"var ts1 = msg.payload; // some other time\nvar diffMS = ts1 - Date.now();\nvar diffS = diffMS / 1000;\nmsg.payload = diffS;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1300,"y":180,"wires":[["e34dd9b5.008118"]]},{"id":"15997279.16c56e","type":"inject","z":"b83d9b41.9c80c8","name":"not a timestamp","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"i am not a number","payloadType":"str","x":1120,"y":140,"wires":[["d6daa69e.7b89e8"]]}]

```

---

<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:** [2 December 2020 10:04 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/6 "2020-12-02T10:04:36Z")

</div>

> [@RASelkirk](#):
>
> I went back and tried to find it with no luck

Tried to find what?

---

<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:** [31 January 2021 10:05 UTC](https://discourse.nodered.org/t/time-subtraction-in-a-function-node/36774/7 "2021-01-31T10:05:04Z")

</div>

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