# How to calculate time difference between two timestamps?

**URL:** <https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550>\
**Category:** General\
**Created:** [10 September 2022 11:19 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550 "2022-09-10T11:19:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [10 September 2022 11:19 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/1 "2022-09-10T11:19:15Z")

</div>

I have a timestamp:

```auto
previousTime: "2022-09-10T08:40:00.000Z"

```

I want to calculate the time elapsed since the timestamp.  
For example if the current time is

```auto
currentTime: "2022-09-10T10:44:00.000Z"

```

I want to output a string that looks like this:

```auto
2 hours and 4 minutes ago.

```

I do not care for days, months or years because the difference will never be more than a few hours, also I don't need to display seconds. Only hours and minutes.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 September 2022 12:14 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/2 "2022-09-10T12:14:46Z")

</div>

In a function node you could do.

```auto
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)
msg.payload = hours > 0 ? `${hours} hours and ` : "";
msg.payload += `${mins} minutes ago.`
return msg;

```

[edit] made output deal with hours = 0. Also change round to floor for hours.

---

<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 September 2022 12:17 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/3 "2022-09-10T12:17:37Z")

</div>

Edit - @E1cid already answered.

A Function node can calculate the elapsed time in seconds and then convert it to hh:mm:ss eg "03:39:10"

```auto
function toTime(seconds) {
    var date = new Date(null);
    date.setSeconds(seconds);
    return date.toISOString().substr(11, 8);
}
let previousTime = new Date("2022-09-10T08:40:00.000Z");
let currentTime = new Date();
let diffseconds = (currentTime.getTime() - previousTime.getTime())/1000;
msg.payload = toTime(diffseconds);
return msg;

```

I got the toTime() function from [stackoverflow](https://stackoverflow.com/questions/37096367/how-to-convert-seconds-to-minutes-and-hours-in-javascript). There are some varied solutions there if you want to produce a different format.

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [10 September 2022 17:28 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/4 "2022-09-10T17:28:37Z")

</div>

Thank you, worked perfectly 🙂

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [10 September 2022 18:08 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/5 "2022-09-10T18:08:04Z")

</div>

Actually, it worked for one of my flows but not for another:

```auto
msg.payload.lastupdated: "2022-09-10T17:54:27.795"

```

```auto
msg.timestamp: "2022-09-10T18:04:23.173Z"

```

```auto
let current = new Date(msg.timestamp).valueOf();
let previous = new Date(msg.payload.lastupdated).valueOf();
let diff = current - previous;
let mins = Math.round((diff % 3600000) / 60000);
let hours = Math.floor(diff / 3600000)
msg.relativeTime = hours > 0 ? `${hours} hours and ` : "";
msg.relativeTime += `${mins} minutes ago.`
return msg;

```

This should output "10 minutes ago" but it outputs "2 hours and 10 minutes ago."

Why? My timezone is UTC+2 but both of the timestamps are in UTC so why does it add the two hours?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 September 2022 18:24 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/6 "2022-09-10T18:24:05Z")

</div>

Might be because msg.payload.lastupdated time doesn't have a `Z` at the end...

---

<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:** [10 September 2022 18:29 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/7 "2022-09-10T18:29:17Z")

</div>

Yes, it will assume local timezone if you don't give it one.

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [10 September 2022 18:50 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/8 "2022-09-10T18:50:12Z")

</div>

Yes, that was it! Added a "Z" to the end and now it works perfectly. Thanks.

---

<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:** [24 September 2022 18:50 UTC](https://discourse.nodered.org/t/how-to-calculate-time-difference-between-two-timestamps/67550/9 "2022-09-24T18:50:33Z")

</div>

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