# Issues calculating time between two timestamps

**URL:** <https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 November 2022 13:17 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921 "2022-11-20T13:17:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aperture1988](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aperture1988/32/40681_2.png) [@aperture1988](https://discourse.nodered.org/u/aperture1988)\
**Post date:** [20 November 2022 13:17 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/1 "2022-11-20T13:17:08Z")

</div>

Hello Guys,

Hope everyone is well, I wonder if some one could assist me here, apologies in advance I'm very new to coding function nodes.

Im in the process of trying to turn my dumb paper shredder into a somewhat smart device so I can trigger proactive alerts when the device needs to be serviced.

In order to achieve this I need to work out the the time in seconds between when the machine is running and when it shuts off. Ive kind of got what I need but the formatting isn't correct.

My current flow will trigger when the device power consumption is over a set value, the flow injects a timestamp when it starts, it then waits for the power consumption to drop and then my function node works out of the time between the start and finish timestamps.

The code below seems to work however I producing an example output of -13.972

so my questions are:

1.) Im unsure why it is returning a negative value and how would I change it so the output is 13.972  
2.) How would I then adapt my code so it rounds to the nearest second? in this example 14

```auto
var starttime = msg.starttime;
var diffMS = starttime - Date.now();
var diffS = diffMS / 1000;
msg.usage = diffS;
return msg;

```

 ![Screenshot 2022-11-20 at 13.12.25](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c69c09f72e68a5e44bc27869d04fd5da82b1ab1.png)

Thanks in advance.

---

<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:** [20 November 2022 13:38 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/2 "2022-11-20T13:38:02Z")

</div>

> [@aperture1988](#):
>
> `var diffMS = starttime - Date.now();`

You are taking the current time away from the start time. Current time is larger than the start time. Try  
`Date.now() - starttime`

---

<div class="post-metadata">

**Author:** ![aperture1988](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aperture1988/32/40681_2.png) [@aperture1988](https://discourse.nodered.org/u/aperture1988)\
**Post date:** [20 November 2022 13:42 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/3 "2022-11-20T13:42:49Z")

</div>

Thank you, that worked perfectly what a noob error.

Any ideas how I can round the value to the nearest second?

---

<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:** [20 November 2022 13:45 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/4 "2022-11-20T13:45:48Z")

</div>

Look at the [javascript `round()` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round).

---

<div class="post-metadata">

**Author:** ![aperture1988](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/aperture1988/32/40681_2.png) [@aperture1988](https://discourse.nodered.org/u/aperture1988)\
**Post date:** [20 November 2022 14:15 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/5 "2022-11-20T14:15:58Z")

</div>

Thank you Colin have this working now,

If anyone is interested:

```auto
var starttime = msg.starttime;
var diffMS = Date.now() - starttime;
var diffS = diffMS / 1000;
var round = Math.round(diffS)
msg.usage = round;
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:** [20 November 2022 14:53 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/6 "2022-11-20T14:53:58Z")

</div>

You should generally not use `var` nowadays, it is better to use `const` or `let`. It won't make any difference in this case but it is better to use the newer directives.

---

<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:** [4 December 2022 14:54 UTC](https://discourse.nodered.org/t/issues-calculating-time-between-two-timestamps/70921/7 "2022-12-04T14:54:13Z")

</div>

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