# Trouble storing time in flow variable

**URL:** <https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715>\
**Category:** General\
**Created:** [4 December 2021 17:30 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715 "2021-12-04T17:30:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [4 December 2021 17:30 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/1 "2021-12-04T17:30:08Z")

</div>

I am trying to store time variables in flow variables. And somehow it's not working. When I test with node.warn, everything seems ok. Also when I store actual time, it's ok. So I am puzzled here. Can anybody help me out?

```auto
msg_p = {};
msg_period = {};
msg_datepick = {};
let h, m, y, val, period, d;

// Start up
if (msg.topic == "boot"){
}

flow.set('overview.actualtime', new Date());    
// Button today pushed
if (msg.topic == "today"){
    period = "today";
    flow.set('overview.period', period);    
    // Set new starttime    
    d = new Date();
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
node.warn("starttime = "+d);
    flow.set('overview.starttime', d);    
    // Set new endtime    
    d.setHours(23);
    d.setMinutes(59);
    d.setSeconds(59);
node.warn("endtime = "+d);
    flow.set('overview.endtime', d);    
}

```

The result in flow variables is,

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/dac35f1082f89367c60a03d483a95a86ac36e60b.png)

as you can see, starttime and endtime are the same here.

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91fe7f7de8aa8c0201bb74588c631fc811a48d90.png)

In node.warn the result looks good.

🤨

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [4 December 2021 17:42 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/2 "2021-12-04T17:42:36Z")

</div>

I tried it another way, and now the node.warn is also wrong, same as flow variables.

```auto
msg_p = {};
msg_period = {};
msg_datepick = {};
let h, m, y, val, period, d, ds, de;

// Start up
if (msg.topic == "boot"){
}

flow.set('overview.actualtime', new Date());    
// Button today pushed
if (msg.topic == "today"){
    period = "today";
    flow.set('overview.period', period);    
    // Set new starttime    
    d = new Date();
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    ds = d;
    // Set new endtime    
    d.setHours(23);
    d.setMinutes(59);
    d.setSeconds(59);
    de = d;
node.warn("starttime = "+ds);
node.warn("endtime = "+de);
    flow.set('overview.starttime', ds);    
    flow.set('overview.endtime', de);    
}

```

![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/7707253102af1d37a9adc5d0b2e358705928a63d.png)

---

<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:** [4 December 2021 17:52 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/3 "2021-12-04T17:52:32Z")

</div>

This is due to JS storing objects by reference. you have set endtime and starttime to refer to the same object. When you update the object starttime refers to, then when you update endtime you update the same object. Simple fix is to use different Date vars

```auto
msg_p = {};
msg_period = {};
msg_datepick = {};
let h, m, y, val, period, ds, de;

// Start up
if (msg.topic == "boot"){
}

flow.set('overview.actualtime', new Date());    
// Button today pushed
if (msg.topic == "today"){
    period = "today";
    flow.set('overview.period', period);    
    // Set new starttime    
    ds = new Date();
    ds.setHours(0);
    ds.setMinutes(0);
    ds.setSeconds(0);
node.warn("starttime = "+ds);
    flow.set('overview.starttime', ds);    
    // Set new endtime    
    de = new Date();
    de.setHours(23);
    de.setMinutes(59);
    de.setSeconds(59);
node.warn("endtime = "+de);
    flow.set('overview.endtime', de);    
}
return null;

```

> **[How to get a grip on reference vs value in JavaScript](https://www.freecodecamp.org/news/how-to-get-a-grip-on-reference-vs-value-in-javascript-cba3f86da223/)**
>
> by Marina Ferreira How to get a grip on reference vs value in JavaScriptThis article discusses how the various JavaScript data types behave when they’re assigned to a variable. Depending on the data type, memory is allocated differently to store it....

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [4 December 2021 18:04 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/4 "2021-12-04T18:04:04Z")

</div>

@E1cid , yes, indeed, now I understand. I already knew for copying objects (deep copying). But did not connect the dots, this is also an object. Thanks very much, helpt me a lot.

---

<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:** [4 December 2021 18:24 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/5 "2021-12-04T18:24:02Z")

</div>

Somewhat of a dated tutorial now. There are other, better methods to create a clone from ES6 onwards.

---

<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:** [4 December 2021 19:18 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/6 "2021-12-04T19:18:35Z")

</div>

The link was more for the OP to understand how objects are stored by reference, not for the cloning technics. If you wish to add a link for better cloning methods feel free.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [5 December 2021 13:57 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/7 "2021-12-05T13:57:35Z")

</div>

@TotallyInformation, @E1cid, yes, I read the article, it was very technical, but I understand a bit.  
I think copying objects in javascript is a weard thing though. I know the concept of alliasing, that's very clear.  
But normal copying of objects in javascript is a mix of copying and aliassing.

I use then:

`flow.set('dest', JSON.parse(JSON.stringify(source))); // Deep copy);`

But now I have another mistery on my hands with the same subject I think.  
Maybe you can help me out with that also, here is the code,

```auto
let test = {};
let a,b,c;

a=1;
b=2;
c=3;

// This is working
test.a = a;
test.b = b;
test.c = c;

// This is NOT working
//test.item.a = a;
//test.item.b = b;
//test.item.c = c;

flow.set('test', JSON.parse(JSON.stringify(test))); // Deep copy);

//flow.set('dest', JSON.parse(JSON.stringify(source))); // Deep copy);

//node.warn(test);
//node.warn(typeof test);

return msg;

```

If I use test.item.a/b/c, I get this warning,  
 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2ff7ac1975d350b8062e8b11de1c2ebe2e8f304d.png)

What I am trying to do , is build a complex object, and then (deep) copy the complete object to flow.  
But if I write each (complex) variable separete to flow it is working, but I don't know if that is referenced then or a real copy.

This is working:

`flow.set('test.item.a', a );`

---

<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:** [5 December 2021 14:10 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/8 "2021-12-05T14:10:40Z")

</div>

> [@henkkas](#):
>
> `test.item.a = a;`

That will fail because test.item does not exist, so you cannot set a property in it. If you first did  
`test.item = {}`  
then it should be ok.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [5 December 2021 14:19 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/9 "2021-12-05T14:19:42Z")

</div>

@Colin , indeed, but then I have to define every object in an object, but if I do like this,

`flow.set('test.item.a', a );`

It is working.  
So writing each variable seperately????  
if I am working this way, is it a copy or a reference?

---

<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:** [5 December 2021 14:32 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/10 "2021-12-05T14:32:50Z")

</div>

You could use [Object.assign() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) to add to the stored object.

or

```auto
let key = "a";
let a = "a value";
flow.set('test.item.' + key, a );

```

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [5 December 2021 14:41 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/11 "2021-12-05T14:41:20Z")

</div>

@E1cid , your solution might be working, I also read the article you pointed out, but that doesn't make live easier.  
I like simple code. So I think I opt for the solution to copy each variable seperately.

---

<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:** [5 December 2021 15:26 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/12 "2021-12-05T15:26:55Z")

</div>

> [@henkkas](#):
>
> flow.set('dest', JSON.parse(JSON.stringify(source))); // Deep copy);

Make absolutely certain that you wrap that line in a try/catch block for when it fails. JSON parsing is very fragile. It is generally better to use something like `flow.set('test', Object.assign({}, test))`

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [5 December 2021 15:33 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/13 "2021-12-05T15:33:07Z")

</div>

@TotallyInformation, I guess you speak from experience, so I will replace that construct at the places I used it. Thanks very much for the info so far.

Also thanks @E1cid and @Colin for your input.  
I am still learning every day!! 😀 😀

---

<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:** [19 December 2021 15:33 UTC](https://discourse.nodered.org/t/trouble-storing-time-in-flow-variable/54715/14 "2021-12-19T15:33:50Z")

</div>

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