# Function node - weird date behaviour?

**URL:** https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672
**Category:** General
**Created:** [3 May 2024 06:49 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672 "2024-05-03T06:49:55Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [3 May 2024 06:49 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/1 "2024-05-03T06:49:55Z")

</div>

I have a function node with the following:

```auto
node.warn(new Date(2024, 4, 1))
node.warn(new Date(2024, 4, 1).toJSON())

```

output:

```auto
function : (warn)
"Wed May 01 2024 00:00:00 GMT+0200 (Central European Summer Time)"

function : (warn)
"2024-04-30T22:00:00.000Z"

```

I must be missing something, but what goes wrong here ?  
(corrected the first output, copied the wrong one)

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [3 May 2024 07:01 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/2 "2024-05-03T07:01:16Z")

</div>

There nothing wrong here.

The first implicitly uses Date toString(), so the output is formatted according to the locale.

On the other hand, toJSON() uses toISOString(), which results in UTC in ISO 8601 notation.

For reference: [Date.prototype.toJSON() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [3 May 2024 07:02 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/3 "2024-05-03T07:02:10Z")

</div>

Yes, but the first is first of May, second is 30th of April.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [3 May 2024 07:03 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/4 "2024-05-03T07:03:50Z")

</div>

Because the time of the date is 0:00 in UTC+2. (note: your local timezone)  
If converted to UTC this results in the prior day at 22:00.

Nothing wrong here, just two different timezones 🙂

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [3 May 2024 07:05 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/5 "2024-05-03T07:05:39Z")

</div>

Ah, missed to process that part in the brain, got it thanks!

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [3 May 2024 07:08 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/6 "2024-05-03T07:08:39Z")

</div>

Yes, it's a bit tricky.  
If you don't specify the timezone when using the Date function or constructor, the local timezone is used.

I recommend using UTC where possible to avoid those problems.

---

<div class="post-metadata">

### Author: ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)
#### Post date: [3 May 2024 07:13 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/7 "2024-05-03T07:13:07Z")

</div>

nothing is wrong it looks perfect

what do you want to do?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [3 May 2024 07:16 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/8 "2024-05-03T07:16:43Z")

</div>

Indeed - for this function i needed to get the first and last date of each month, I now specified 10:00 and then i hopefully will be good, but will make it a bit more robust.

---

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [3 May 2024 07:20 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/9 "2024-05-03T07:20:45Z")

</div>

> [@bakman2](#):
>
> first and last date of each month

I found this at [stackoverflow](https://stackoverflow.com/questions/222309/calculate-last-day-of-month#222439) which uses no time at all.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [3 May 2024 07:25 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/10 "2024-05-03T07:25:05Z")

</div>

Indeed i used something similar, but wanted to check the output (as i need to calculate the date to another number) and found this difference, which was basically self-inflicted I guess 😉

---

<div class="post-metadata">

### Author: ![homeplayer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/homeplayer/32/12388_2.png) [@homeplayer](https://discourse.nodered.org/u/homeplayer)
#### Post date: [3 May 2024 07:25 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/11 "2024-05-03T07:25:21Z")

</div>

> [@bakman2](#):
>
> Indeed - for this function i needed to get the first and last date of each month, I now specified 10:00 and then i hopefully will be good, but will make it a bit more robust.

```auto
const getMonthInfo = () => {
    const currentDate = new Date();
    const firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
    const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
    
    const monthName = currentDate.toLocaleString('default', { month: 'long' });

    return {
        nr: currentDate.getMonth() + 1,
        name: `${monthName} - First: ${firstDayOfMonth.toDateString()}, Last: ${lastDayOfMonth.toDateString()}`
    };
};
return { payload: getMonthInfo()}

```

```auto
payload: object
nr: 5
name: "May - First: Wed May 01 2024, Last: Fri May 31 2024"

```

---

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [3 May 2024 07:29 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/12 "2024-05-03T07:29:22Z")

</div>

Ah sorry, quite - you are already using just date. Weird how JS adds a time to a date - other languages don't do that, instead they have a "pure" [date type](https://docs.python.org/3/library/datetime.html#date-objects) as opposed to a datetime type or just a time type.

---

<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: [3 May 2024 10:12 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/13 "2024-05-03T10:12:14Z")

</div>

> [@gregorius](#):
>
> Weird

JS date handling and processing is a known problem drenched in history and ...

Hopefully it won't be too much longer when we can switch to [temporal](https://tc39.es/proposal-temporal/docs/) (and get rid of (or at least reduce the need for) all the other libs like moment!)

FWIW, you can use temporal today if you desire: [@js-temporal/polyfill - npm](https://www.npmjs.com/package/@js-temporal/polyfill)

---

<div class="post-metadata">

### Author: ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)
#### Post date: [3 May 2024 11:32 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/14 "2024-05-03T11:32:34Z")

</div>

Thanks for that, learnt something new: [polyfill](https://en.wikipedia.org/wiki/Polyfill_(programming)):

> What makes a polyfill different from the techniques we have already, like a shim, is this: if you removed the polyfill script, your code would continue to work, without any changes required _in spite of the polyfill being removed._

Didn't know.

---

<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: [17 May 2024 11:32 UTC](https://discourse.nodered.org/t/function-node-weird-date-behaviour/87672/15 "2024-05-17T11:32:46Z")

</div>

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