# Need help with a sub-flow and dates (sorry folks)

**URL:** <https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740>\
**Category:** General\
**Created:** [6 March 2020 21:35 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740 "2020-03-06T21:35:59Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 March 2020 20:51 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/21 "2020-03-07T20:51:00Z")

</div>

Hi Julian.  
Yeah, sorry: my bad with the title.

Yes, I do get the idea of using Zulu time, and it is nice if you live near there and not the other side of the world.

Because I am wanting to see times in LOCAL time, I am trying to get a `timestamp` node that works.

In the node I have the locale set as `en_AU` and print the time output as `YYYY-MM-DD hh:mm:ss`

Well, I think the output is actually `YYYY/MM/DD:hh:mm:ss` and I _reformat_ that to a file friendly format without / or colons.

So when my flow outputs the incorrect date, I'm confused.

When I went to your github page, my post isn't posted/wasn't posted. Which is strange, but I am not fluent with how github works with posts.

Not wanting to flood the thread with repetitive things I'll hold back on posting more again.

---

<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:** [7 March 2020 21:25 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/22 "2020-03-07T21:25:52Z")

</div>

> [@Trying\_to\_learn](#):
>
> Yes, I do get the idea of using Zulu time, and it is nice if you live near there and not the other side of the world.

That is irrelevant. You don't normally need to _see_ UTC timestamps, as @TotallyInformation said you should always work _internally_ in UTC and convert to local time when you want to display the time. Therefore usually all the timestamp nodes need to be able to do is to convert from a UTC timestamp (in the form of a javascript Date object or as an ISO string) to local time for display and possibly vice-versa. Are you attempting to stick to that convention?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 March 2020 21:38 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/23 "2020-03-07T21:38:02Z")

</div>

Slight update to all reading:

The original _sub flow_ had a `function` node to generate the time stamp.

It was suggested I use a `change` node instead.

I did that and it seems to be more behaved.

So from that:  
Is there a problem with the time stamp the `function` node created compared to what is given with the `change` node setting the payload to `time stamp`?

Some stuff to affirm this theory:

![Screenshot from 2020-03-08 09-04-18](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e1a7fdac4e0f51221fe4f404829e83daf4456b24.png)

Code:

```auto
[{"id":"81bf6744.e2fca","type":"change","z":"a941e919.4a7d88","name":"Save","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":490,"wires":[["69d34dee.2327ec"]]},{"id":"8b0f2a2d.ac1c2","type":"inject","z":"a941e919.4a7d88","name":"","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":490,"wires":[["81bf6744.e2fca"]]},{"id":"69d34dee.2327ec","type":"change","z":"a941e919.4a7d88","name":"TimeStamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":530,"wires":[["d4ea0006.7a56"]]},{"id":"d4ea0006.7a56","type":"moment","z":"a941e919.4a7d88","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_AU","output":"payload","outputType":"msg","outTz":"Australia/Sydney","x":490,"y":530,"wires":[["61d6d5f1.136694","90a868bf.d38458","342679c5.96e6de"]]},{"id":"61d6d5f1.136694","type":"string","z":"a941e919.4a7d88","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":":"},{"type":"str","value":""}]},{"name":"replaceAll","params":[{"type":"str","value":" "},{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":540,"y":630,"wires":[["bf6026.2cf297d8"]]},{"id":"90a868bf.d38458","type":"change","z":"a941e919.4a7d88","name":"TOPIC","rules":[{"t":"move","p":"payload","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":580,"wires":[["f1cf49e4.32faa"]]},{"id":"342679c5.96e6de","type":"debug","z":"a941e919.4a7d88","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":530,"wires":[]},{"id":"bf6026.2cf297d8","type":"debug","z":"a941e919.4a7d88","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":630,"wires":[]},{"id":"f1cf49e4.32faa","type":"change","z":"a941e919.4a7d88","name":"Get","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":580,"wires":[["3395b9b.e481046"]]},{"id":"3395b9b.e481046","type":"debug","z":"a941e919.4a7d88","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"time","targetType":"msg","x":730,"y":580,"wires":[]},{"id":"b6883877.9c4ad8","type":"function","z":"a941e919.4a7d88","name":"TimeStamp","func":"var time = new Date().toLocaleString();\nmsg.time = time;\nmsg.payload = time;\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":710,"wires":[["6657d703.a6a4a8"]]},{"id":"6657d703.a6a4a8","type":"moment","z":"a941e919.4a7d88","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en_AU","output":"payload","outputType":"msg","outTz":"Australia/Sydney","x":490,"y":710,"wires":[["6d5014b1.2e7024","7d832e19.919f58","b8bdd8fd.618978"]]},{"id":"6d5014b1.2e7024","type":"string","z":"a941e919.4a7d88","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":":"},{"type":"str","value":""}]},{"name":"replaceAll","params":[{"type":"str","value":" "},{"type":"str","value":""}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":540,"y":810,"wires":[["4638ff0.73c24"]]},{"id":"7d832e19.919f58","type":"change","z":"a941e919.4a7d88","name":"TOPIC","rules":[{"t":"move","p":"payload","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":760,"wires":[["749449c5.91cc1"]]},{"id":"b8bdd8fd.618978","type":"debug","z":"a941e919.4a7d88","name":"A","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":710,"wires":[]},{"id":"3e45789d.28fdc8","type":"change","z":"a941e919.4a7d88","name":"Save","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":670,"wires":[["b6883877.9c4ad8"]]},{"id":"4638ff0.73c24","type":"debug","z":"a941e919.4a7d88","name":"C","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":730,"y":810,"wires":[]},{"id":"749449c5.91cc1","type":"change","z":"a941e919.4a7d88","name":"Get","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":760,"wires":[["1063b383.46170c"]]},{"id":"a540d96d.0ba558","type":"inject","z":"a941e919.4a7d88","name":"","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":670,"wires":[["3e45789d.28fdc8"]]},{"id":"1063b383.46170c","type":"debug","z":"a941e919.4a7d88","name":"B","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"time","targetType":"msg","x":730,"y":760,"wires":[]}]

```

---

<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:** [7 March 2020 22:10 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/24 "2020-03-07T22:10:43Z")

</div>

Andrew, it should be easy to check out. Create a flow with an` inject` node connected `function` node connected to a `debug` node (set to display complete msg object)

Have the `inject` set msg.payload to a timestamp.  
In the `function` node have

```auto
var d = new Date();
var u = d.getTime();
msg.functiondate = u
return msg

```

and look at the results.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [7 March 2020 22:16 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/25 "2020-03-07T22:16:02Z")

</div>

This is getting way above my skill set.

I am posting here with more _information_ only to try and help people who are smarter than me find the problem.

The `change` node sending a timestamp seems to be the better way for getting the flow working.

Again I shall state that the whole idea of the sub-flow _node_ is that you can send anything into it and get a message coming out that is the date/time in the ISO format.  
`YYYY-MM-DD hh mm ss`  
(correct?)

Yes, there is a provision in the sub-flow to pass messages, but that is for local requirements.  
It may be handy for others too.

---

<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:** [7 March 2020 22:34 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/26 "2020-03-07T22:34:14Z")

</div>

> [@Trying\_to\_learn](#):
>
> Is there a problem with the time stamp the `function` node created compared to what is given with the `change` node setting the payload to `time stamp` ?

No...as long as you are comparing apples to apples and not apples to a hamburger.

---

<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:** [8 March 2020 01:37 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/27 "2020-03-08T01:37:44Z")

</div>

> [@Trying\_to\_learn](#):
>
> Again I shall state that the whole idea of the sub-flow _node_ is that you can send anything into it and get a message coming out that is the date/time in the ISO format.

No, that is what the moment node is for not your subflow 🙂

But as I've tried to say, it isn't that easy. It is _impossible_ to be able to send _ANY_ timestamp and have it converted. Unless you also know the input format. MomentJS certainly can't do it, that's why I use a separate package to extend its ability to parse inputs. But even that cannot be perfect as is mentioned in the help information for the moment node.

---

<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:** [8 March 2020 07:35 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/28 "2020-03-08T07:35:10Z")

</div>

An example of why it is not possible is that there is no way to know what 02/03/20 means without information on the format of the data coming in.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 March 2020 20:32 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/29 "2020-03-08T20:32:13Z")

</div>

When I create the _time stamp_ in the `function` node, with this code:  
`var time = new Date().toLocaleString();`

`LOCALESTRING` So to me that implies time zone is a given.  
Ok, I have been wrong before.

When I use the `change` node and set the timestamp in there, the flow works better.

So why is there that disparity in what happens?

To me that needs to be explained to me - if someone wants to - because as far as I can take it:  
there lies a problem.

Post 23 shows that clearly.

---

<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:** [8 March 2020 21:01 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/30 "2020-03-08T21:01:06Z")

</div>

Hi, you don't need to put the toLocaleString() on the end, it is not needed. If you leave it off, you can pass the date object directly out to the moment node and let it convert to the appropriate locale/timezone.

When you add the function, it does indeed use the timezone BUT, it doesn't pass it out to the ongoing msg because locale is expected to be used for display as we've previously said.

> [@Trying\_to\_learn](#):
>
> To me that needs to be explained to me - if someone wants to - because as far as I can take it:  
> there lies a problem.

Sorry, but we've already done this. You've output an ambiguous date/time, the parse function had to make an assumption which unfortunately for you is the wrong assumption for your use. All avoided by using UTC/ISO.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [9 March 2020 21:58 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/31 "2020-03-09T21:58:26Z")

</div>

Well,

> [@TotallyInformation](#):
>
> Sorry, but we've already done this.

I'm not understanding it.

I guess I shall just have to live with the ignorance of not knowing. Though I was already doing that before I asked. I just didn't know I didn't know.

---

<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 March 2020 22:11 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/32 "2020-03-09T22:11:33Z")

</div>

> [@TotallyInformation](#):
>
> ambiguous date/time

Is the key.

Human readable dates are ambiguous when applied across different localities. Computers don't deal well with ambiguity.

While _you_ know the timezone/locality that date is in, the computer doesn't because you haven't told it.

I made the node guess based on where I live. Sadly, you are on the opposite side of the globe I think.

The solution is either to provide the input in an unambiguous format or to pass in the source timezone & locale.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [9 March 2020 22:17 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/33 "2020-03-09T22:17:09Z")

</div>

> [@TotallyInformation](#):
>
> Human readable dates are ambiguous when applied across different localities. Computers don't deal well with ambiguity.

And I fully agree.  
Which is why there are _time zone_ things, etc. Locale.... et al.

So I don't get why:  
I get a `function` node and set it up to give the _time_ locally. (Now)  
I input that to the `moment` node and get it to spit out YYYY-MM-DD hh.mm.ss (slight change of punctuation, but I don't think that is really relevant).

I get the _wrong_ output.

However, if I replace the `function` node with a `change` node and get it to make the msg.payload the _timestamp_ it works.

You say you have explained it, and I can't/won't dispute that.  
I am not understanding it. Be it I am thicker than you gave me credit, or what ever.

The _problem_ has been resolved by using the `change` node.  
Why/how? Can't say. I don't think I am going to get it. Such is life.

> [@TotallyInformation](#):
>
> The solution is either to provide the input in an unambiguous format or to pass in the source timezone & locale.

That was what I thought I was doing. But again: I am wrong.

I can only do what I understand and know.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 March 2020 22:41 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/34 "2020-03-09T22:41:42Z")

</div>

The code you used in the Function node was:

```auto
var time = new Date().toLocaleString();

```

In my locale that creates a string that looks like: `"09/03/2020, 22:37:20"`

When you pass that to the Moment node, it does not know if that string is formatted as MM/DD/YY or DD/MM/YY because both interpretations are valid - 9th March or 3rd September. The node defaults to the UK interpretation because that's what Julian decided it should do.

When you use the Change node, its sets the property to a timestamp - `1583793549617`. This is the number of milliseconds since 00:00 GMT January 1st 1970. This timestamp is unambiguous. The moment node can take that value, know _exactly_ what time it represents and then present it in your local timezone appropriately.

Does that help explain the difference between your Function node and Change node approaches? Yes they are both generating a date, but the way they are representing that date are fundamentally different.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [9 March 2020 22:59 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/35 "2020-03-09T22:59:23Z")

</div>

I'll have to think about it for a while.

I'm not have a good run with brain reliability just now. ☹

---

<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 March 2020 23:40 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/36 "2020-03-09T23:40:24Z")

</div>

> [@Trying\_to\_learn](#):
>
> I get a `function` node and set it up to give the _time_ locally. (Now)  
> I input that to the `moment` node and get it to spit out YYYY-MM-DD hh.mm.ss (slight change of punctuation, but I don't think that is really relevant).
> 
> I get the _wrong_ output.

Because the inputs are different. One is ambiguous and the other is not. Take the locale part off your function output and you will get the same as the output from the change node.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [10 March 2020 05:13 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/37 "2020-03-10T05:13:23Z")

</div>

> [@TotallyInformation](#):
>
> The solution is either to provide the input in an unambiguous format or to pass in the source timezone & locale

A third possibility is to move to UK (if BoJo will let us in)

---

<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 March 2020 07:15 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/38 "2020-03-10T07:15:04Z")

</div>

As has been advised more than once, take off the `.toLocaleString()`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2020 08:19 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/39 "2020-03-10T08:19:08Z")

</div>

Ok. As you may have seen I am now with another problem with a machine giving an error when I run Node-red.

The `function` node has been replaced with a `change` node.

---

<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:** [10 March 2020 09:19 UTC](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/40 "2020-03-10T09:19:40Z")

</div>

> [@krambriw](#):
>
> move to UK

Honestly, I think it more likely that we will want to join others! I hear that Finland is nice 😀 Only been there once and only got to see out the taxi window from the airport to the hotel and back.

[Previous page](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740.md?page=1)

[Next page](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740.md?page=3)
