# Cannot calculate date difference in NodeRed?!

**URL:** <https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257>\
**Category:** General\
**Created:** [19 September 2018 07:07 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257 "2018-09-19T07:07:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![diversit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diversit/32/2678_2.png) [@diversit](https://discourse.nodered.org/u/diversit)\
**Post date:** [19 September 2018 07:07 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/1 "2018-09-19T07:07:38Z")

</div>

Hi,

I'm writing a function where I want to calculate the difference between when current message was received and a previous message. The previous date timestamp is stored in a context variable.

When I do:

```auto
var previous = context.get('prev') || 0;
var now = new Date().getTime();
var since = now - previous;

```

The `since` is always equal to `now`. It is not performing the substration --\> huh??  
I have tried with Date's before, which should also just work, but it also did not.  
Also tried `Math.abs(now - previous)` but it gives the same result.

If, on the same spot, I do a simple `var since = 10 - 9` then the result is `1`.  
I have checked the types. Both `now` and `previous` are of type `number`.

Is this causing an issue because the timestamp value is too large? But why would that be an issue in NodeJS and not in a browser? I use Chrome Console for trying stuff out and there `new Date() - new Date()` just works as expected.  
Is this a known problem with NodeJS/NodeRed?  
How can I calculate the time difference?

Cheers

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 September 2018 07:14 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/2 "2018-09-19T07:14:23Z")

</div>

Somewhere you'll need to store this `previous` value in context. If it is not stored, the value will be always 0 (zero) and your calculation does always the `now - 0 = now`

---

<div class="post-metadata">

**Author:** ![diversit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diversit/32/2678_2.png) [@diversit](https://discourse.nodered.org/u/diversit)\
**Post date:** [19 September 2018 09:20 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/3 "2018-09-19T09:20:15Z")

</div>

`previous` is stored in context and when I log the `previous` value, I see it has the right value.  
So it's value not `0`.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [19 September 2018 09:22 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/4 "2018-09-19T09:22:15Z")

</div>

Can you show whole function you have written then?

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [20 September 2018 13:09 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/5 "2018-09-20T13:09:12Z")

</div>

Hi @diversit  
you don't need a function for that.

Try this and just replace the "Trigger" buttons by your inputs:

```
[{"id":"60a4cc8c.fbd7a4","type":"inject","z":"95476864.681758","name":"previous ","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":140,"wires":[["cddeddfc.246d5"]]},{"id":"16558fab.c3191","type":"inject","z":"95476864.681758","name":"Trigger","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":200,"wires":[["7f4f5da8.834c14"]]},{"id":"cddeddfc.246d5","type":"change","z":"95476864.681758","name":"Save previous time","rules":[{"t":"set","p":"previous_timestamp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":140,"wires":[[]]},{"id":"7f4f5da8.834c14","type":"change","z":"95476864.681758","name":"Calculate","rules":[{"t":"set","p":"payload","pt":"msg","to":"$millis()-$flowContext(\"previous_timestamp\")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":200,"wires":[["66684540.e4291c"]]},{"id":"66684540.e4291c","type":"debug","z":"95476864.681758","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":630,"y":200,"wires":[]}]
```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [20 September 2018 13:19 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/6 "2018-09-20T13:19:00Z")

</div>

> [@machadotiago](#):
>
> Try this and just replace the "Trigger" buttons by your inputs:

I think @diversit wants the time between 2 sequential messages in the same flow - not the time between two messages in different flows

---

<div class="post-metadata">

**Author:** ![diversit](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diversit/32/2678_2.png) [@diversit](https://discourse.nodered.org/u/diversit)\
**Post date:** [21 September 2018 14:16 UTC](https://discourse.nodered.org/t/cannot-calculate-date-difference-in-nodered/3257/7 "2018-09-21T14:16:20Z")

</div>

Sorry guys, my function was a bit more complex as I described here and I just found the problem. All my bad. I reset the previous value to 0 in a function before doing the calculation. Oepsedaisy.  
Thanks for helping out though!
