# Date object in a function node not created

**URL:** <https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623>\
**Category:** General\
**Created:** [9 November 2025 10:01 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623 "2025-11-09T10:01:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [9 November 2025 10:01 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/1 "2025-11-09T10:01:34Z")

</div>

I got a function node which collects MQTT Status messages from my zigbee2mqtt Broker. In particular I want the `last_seen` time stamp to monitor my zigbee network.  
when a new device is detected an array is extended like:

```auto
dlist.push({ 
        name: device, 
        status: msg.payload.state, 
        time: new Date(msg.payload.last_seen), 
        ignored: false, 
        alarm: false })

```

The problem is the `dlist.time` value. Despite that its constructed as a date object it is inserted as a date/time string like `2025-11-09T09:46:02.000Z`

`msg.payload.last_seen` is a date string but I want a date object in the table. Why is this conversion not working?

---

<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:** [9 November 2025 10:09 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/2 "2025-11-09T10:09:33Z")

</div>

if `msg.payload.last_seen` really is a string containing exactly `2025-11-09T09:46:02.000Z` then there is no reason that wouldn't work

Proof in Node REPL:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7d6b398ab2d0c39c8e30f1b6799f7ea64cf4805.png)

So something is not quite right. either the path is wrong or its not actually a string or...

Add the below to your function:

```js
const theType = typeof msg.payload.last_seen
const theValue = msg.payload.last_seen
node.warn({theType, theValue})
dlist.push({ 
        name: device, 
        status: msg.payload.state, 
        time: new Date(msg.payload.last_seen), 
        ignored: false, 
        alarm: false })

```

and show us what you see in debug pnael.

---

<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:** [9 November 2025 10:16 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/3 "2025-11-09T10:16:54Z")

</div>

> [@haegar33](#):
>
> The problem is the `dlist.time` value

Forgot to mention, `dlist.time` will not get you your array element value (pretty sure thats just a typo tho)

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [9 November 2025 10:17 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/4 "2025-11-09T10:17:09Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> node.warn({theType, theValue})
> 
> ```

as expected:

```auto
{ theType: "string", theValue: "2025-11-09T11:14:01+01:00" }

```

This is really odd. When I create a simple test script outside NR it works fine. the array is stored at the end in a global variable:

```auto
global.set("devices", dlist)

```

---

<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:** [9 November 2025 10:17 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/5 "2025-11-09T10:17:55Z")

</div>

> [@Steve-Mcl](#):
>
> > [@haegar33](#):
> >
> > The problem is the `dlist.time` value
> 
> Forgot to mention, `dlist.time` will not get you your array element value

So are you really trying to get the value via `dlist.time`?

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [9 November 2025 10:23 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/6 "2025-11-09T10:23:14Z")

</div>

No for sure. dlist is an array. I don't evaluated that.

When I construct my device table from scratch I have another function to initialise only my zigbee coordinator like

```auto

var d = [{
    name:"Coordinator",
    status: "online",
    time: Date.now(),
    ignored: false,
    alarm: false
}];
global.set("devices",d)

```

This works as expected. the d.time is a date object. All other devices added with the function above are not.

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [9 November 2025 10:30 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/7 "2025-11-09T10:30:50Z")

</div>

![Screenshot 2025-11-09 at 11.29.01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/1/f1ac191e992a90bde463790ccc402c1fc49885d4.png)

The DateConstructor has 3 overloads. Could that be a reason?

---

<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:** [9 November 2025 11:01 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/8 "2025-11-09T11:01:50Z")

</div>

> [@haegar33](#):
>
> ```auto
> var d = [{
> name:"Coordinator",
> status: "online",
> time: Date.now(),
> ignored: false,
> alarm: false
> }];
> global.set("devices",d)
> 
> ```
> 
> This works as expected. the d.time is a date object

`d` is an array. it will never be able to reveal the value of an inner element. `d[0].time` will.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6ef9041087d81299ba5664f00c1d04f41310fa38.png)

> [@haegar33](#):
>
> The DateConstructor has 3 overloads. Could that be a reason?

No. they are there to provide different means of instantiating the date object (by string, by number, by other date object)

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [9 November 2025 12:44 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/9 "2025-11-09T12:44:11Z")

</div>

Ok I think I identified the issue:

The javascript date constructor might work fine however in NR it is always(!) displayed and also exported as a string. You need to represent your date object as number of millisecond since 1970 thus:

` time: Date.parse(msg.payload.last_seen)`

This value can be inspected in the NR debug- and context data windows. And of course it allows date sorting functions to be added.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 November 2025 14:55 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/10 "2025-11-09T14:55:38Z")

</div>

You need to read up a bit on how JavaScript objects are stringified. When a data object is sent to the debug panel or indeed to MQTT, it is automatically converted by JavaScript to an ISO8601 string which is always shown as Zulu time (AKA GMT, AKA UTC).

This is because the communication between JavaScript and HTML (the panel) or MQTT uses JSON, not JavaScript Objects. JSON does not support anything other than JavaScript primitives (number, string, boolean, etc).

---

<div class="post-metadata">

**Author:** ![haegar33](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haegar33/32/38805_2.png) [@haegar33](https://discourse.nodered.org/u/haegar33)\
**Post date:** [9 November 2025 15:01 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/11 "2025-11-09T15:01:26Z")

</div>

"stringified" : nice word for the problem 😉  
I understand this needed for display, JSON export etc. But what about the internal NR variables in flows or global: do the store a` date` object or also only the stringified version?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 November 2025 15:10 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/12 "2025-11-09T15:10:08Z")

</div>

It can get a little complex but in general, when using the default memory based context variables, there is no need for them to be stringified. However, file-based context and possibly others do have to be converted, these are physical limitations not Node-RED ones.

Anywhere that your data has to leave the node.js environment will generally result in a conversion of some sort. This is true for databases as well though they have richer data types to work with, including their own timestamps. So it is less obvious there.

---

<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:** [23 November 2025 15:10 UTC](https://discourse.nodered.org/t/date-object-in-a-function-node-not-created/99623/13 "2025-11-23T15:10:36Z")

</div>

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