# Days between two dates

**URL:** <https://discourse.nodered.org/t/days-between-two-dates/84377>\
**Category:** General\
**Created:** [8 January 2024 04:26 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377 "2024-01-08T04:26:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [8 January 2024 04:26 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/1 "2024-01-08T04:26:29Z")

</div>

Hello

I am trying to calculate the days between two dates.

I have the following:

let date1 = new Date();

Then 2 days later, I set this:

let date2 = new Date();

I have tried:

let number\_of\_days = date2 - date1;

but I get this error: "The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type."

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [8 January 2024 05:34 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/2 "2024-01-08T05:34:02Z")

</div>

Subtracting `date1` from `date2` will return the number of milliseconds between the 2 dates:

```auto
> var d1 = new Date()
2024-01-08T05:19:13.228Z

> var d2 = new Date()
2024-01-08T05:19:25.253Z

> d2 - d1
12025

```

When you say "...two days later..." I would suspect that the original `date1` variable no longer exists as a Date object.

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [8 January 2024 05:46 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/3 "2024-01-08T05:46:52Z")

</div>

> [@shrickus](#):
>
> ```auto
> d2 - d1
> 12025
> 
> ```

It seems to work, but why does it underline the date in red in the function and give me this:

The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [8 January 2024 05:54 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/4 "2024-01-08T05:54:25Z")

</div>

Ah, so the editor is giving that warning -- but the value is still calculated correctly?

In that case, you can get the value of each date in millis and then subtract those:  
`date2.valueOf() - date1.valueOf()`

or use the unary `+` operator to coerce those dates to integers, like this:  
`+date2 - +date1` (although the editor's syntax checker may not like that either)

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [8 January 2024 06:10 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/5 "2024-01-08T06:10:02Z")

</div>

Editor didn't like the first solution, but the second one worked! Thanks!

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [8 January 2024 09:08 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/6 "2024-01-08T09:08:31Z")

</div>

i use this in a function node

```auto
msg.payload = dateToHowManyAgo(msg.payload)

return msg;

function dateToHowManyAgo(stringDate) {
    var currDate = new Date();
    var diffMs = currDate.getTime() - new Date(stringDate).getTime();
    var sec = diffMs / 1000;
    if (sec < 60)
        return parseInt(sec) + ' second' + (parseInt(sec) > 1 ? 's' : '') + ' ago';
    var min = sec / 60;
    if (min < 60)
        return parseInt(min) + ' minute' + (parseInt(min) > 1 ? 's' : '') + ' ago';
    var h = min / 60;
    if (h < 24)
        return parseInt(h) + ' hour' + (parseInt(h) > 1 ? 's' : '') + ' ago';
    var d = h / 24;
    if (d < 30)
        return parseInt(d) + ' day' + (parseInt(d) > 1 ? 's' : '') + ' ago';
    var m = d / 30;
    if (m < 12)
        return parseInt(m) + ' month' + (parseInt(m) > 1 ? 's' : '') + ' ago';
    var y = m / 12;
    return parseInt(y) + ' year' + (parseInt(y) > 1 ? 's' : '') + ' ago';
}

```

---

<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:** [8 March 2024 09:09 UTC](https://discourse.nodered.org/t/days-between-two-dates/84377/7 "2024-03-08T09:09:15Z")

</div>

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