# Maybe a handy \*time stamp\* subflow

**URL:** <https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830>\
**Category:** Share Your Projects\
**Created:** [25 January 2020 03:13 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830 "2020-01-25T03:13:40Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [25 January 2020 03:13 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/1 "2020-01-25T03:13:40Z")

</div>

This node generates a _time stamp_ when it gets an input.

The incoming payload is saved and used on the second output.

The node generates a local time _time stamp_ and presents it to the three outputs as:  
1 - as text which can be used in a text file.  
2 - as `msg.topic` in the same format as output #1  
3 - as text which is compatible to be used in a file name. (Striped `:` from the output)

Yeah, it won't fix every need, but it could be handy.  
Rather than having to constantly use the `moment` node - which I used - and set the output style.

This is a _simple_ addition to what the node does and how it can be used.

 ![Screenshot from 2020-01-25 14-12-31](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/129477a04a57fbdd4f8270514a54d8ae4b83a8f1.png)

```auto
[{"id":"5828ee16.d65a","type":"subflow","name":"Time Stamp","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"38fcac0b.21d234"}]}],"out":[{"x":780,"y":100,"wires":[{"id":"ae0623ca.fcb5e","port":0}]},{"x":780,"y":150,"wires":[{"id":"e0f25625.f2aee8","port":0}]},{"x":780,"y":200,"wires":[{"id":"8897711f.686e08","port":0}]}],"env":[],"color":"#FF8888","outputLabels":["For logging use","msg.time","For filename use"],"icon":"node-red/timer.svg"},{"id":"ba266090.d5a6","type":"function","z":"5828ee16.d65a","name":"Time Stamp","func":"var d = new Date().toLocaleString();\nmsg.payload = d;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":100,"wires":[["ae0623ca.fcb5e"]]},{"id":"ae0623ca.fcb5e","type":"moment","z":"5828ee16.d65a","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-DD-MM HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":500,"y":100,"wires":[["8897711f.686e08","238422be.bf932e"]]},{"id":"8897711f.686e08","type":"string","z":"5828ee16.d65a","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":550,"y":200,"wires":[[]]},{"id":"238422be.bf932e","type":"change","z":"5828ee16.d65a","name":"TOPIC","rules":[{"t":"move","p":"payload","pt":"msg","to":"time","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":150,"wires":[["e0f25625.f2aee8"]]},{"id":"38fcac0b.21d234","type":"change","z":"5828ee16.d65a","name":"Save","rules":[{"t":"set","p":"payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":170,"y":100,"wires":[["ba266090.d5a6"]]},{"id":"e0f25625.f2aee8","type":"change","z":"5828ee16.d65a","name":"Get","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":150,"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:** [25 January 2020 11:09 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/2 "2020-01-25T11:09:47Z")

</div>

@Trying_to_learn Andrew, please edit your post - this is a subflow not a node  
Secondly you can create any of those formats with the `moment` node

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 January 2020 12:10 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/3 "2020-01-25T12:10:57Z")

</div>

> [@zenofmud](#):
>
> Secondly you can create any of those formats with the `moment` node

...or `node-red-contrib-simpletime`

---

<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:** [25 January 2020 21:59 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/4 "2020-01-25T21:59:33Z")

</div>

Errr, no.

I was going to reply to that in the other thread.

I can't do that with the `moment` node as it wants a timestamp input to work.

This doesn't require/expect a timestamp input.

---

<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:** [25 January 2020 22:06 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/5 "2020-01-25T22:06:17Z")

</div>

Thanks @Paul-Reed but that really looks complicated to get an output like what this subflow gives.

I admit/accept I did kind of overlook `simpletime` compared to `moment`. I guess it is down to how I _see_ things.

I just looked at it and though it can give you a heap of options that would entail a lot of concatenating to get the three outputs the subflow gives.

I may keep it in mind for future use. I did have this thought in the back of my mind about _another node to do this_ but just forgot about `simpletime`.

---

<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:** [25 January 2020 22:58 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/6 "2020-01-25T22:58:42Z")

</div>

As I said in the other thread, in the `moment` node you can select the input to be msg.payload OR it can be a time stamp.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [26 January 2020 00:03 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/7 "2020-01-26T00:03:00Z")

</div>

> [@Trying\_to\_learn](#):
>
> Thanks @Paul-Reed but that really looks complicated to get an output like what this subflow gives.

Can you give me an example of what's more complicated in `simpletime` than your sub-flow?  
I've tried to make it as usable as possible, but if there is a problem, maybe it can be improved...

---

<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:** [26 January 2020 07:08 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/8 "2020-01-26T07:08:30Z")

</div>

> [@zenofmud](#):
>
> As I said in the other thread, in the `moment` node you can select the input to be msg.payload OR it can be a time stamp.

I am sorry. Yeah, I see there is that option. But....... As I think I said I just felt like making a more generic _timestamp_ _node_ as I have a lot of `function` nodes doing this function in a few different (and sometimes annoying) ways.

This gives me 3 different formats.

I get that it could be done other ways. I just wanted to go through the motions of writing a _node_ (subflow) to try and improve my understanding of Node-Red.

---

<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:** [26 January 2020 07:13 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/9 "2020-01-26T07:13:33Z")

</div>

> [@Paul-Reed](#):
>
> Can you give me an example of what's more complicated in `simpletime` than your sub-flow?  
> I've tried to make it as usable as possible, but if there is a problem, maybe it can be improved...

Paul,

I am sorry, I didn't mean it as a put down to your node.

I guess it is just a very different way of doing it and when I was doing it, I had overlooked the `simpletime` node and worked with what I saw.

Just another reason - again: not a put down to your node - is that I get 2 different formatted output easily. (The second with the `string` node to ~~replace~~ remove the `:`

Doing it with your node I would need a `function` node after it and then construct what is needed with all the sub-messages.

In the bigger picture, I guess it isn't that much more.  
But then to get the second _format_ message I would have to make a second message, or put it through the `string` node.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [26 January 2020 10:15 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/10 "2020-01-26T10:15:49Z")

</div>

> [@Trying\_to\_learn](#):
>
> Doing it with your node I would need a `function` node after it

Not normally. Just a `template` node is usually needed to create bespoke time formats.

![time](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c32985346bc24a41bbfdef79bb1c81edde14a228.jpeg)  
If you need additional formats - simply add another template node.  
It's a very lightweight solution.

But hey, it's all good fun creating your own solutions 😁

---

<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:** [26 January 2020 11:34 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/11 "2020-01-26T11:34:15Z")

</div>

> [@Trying\_to\_learn](#):
>
> I can't do that with the `moment` node as it wants a timestamp input to work.

Not so. From the help:

> If input is "timestamp" or a JS datetime object or a [string that Moment.js](http://momentjs.com/docs/#/parsing/) can resolve (with the help of [moment.parseFormat](https://github.com/gr2m/moment-parseformat)). It will be processed as normal.

You can pass in any string the that underlying moment node understands. The link is to the docs.

---

<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:** [26 January 2020 22:43 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/12 "2020-01-26T22:43:44Z")

</div>

> [@TotallyInformation](#):
>
> You can pass in any string the that underlying moment node understands. The link is to the docs.

(I must be rewriting the book of stupid, but I shall reply all the same)

All I am reading from what you are saying is that _somewhere_ in the input there must be a _timestamp_ of some sorts.

What if the incoming message has nothing to do with time?

---

<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:** [26 January 2020 22:45 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/13 "2020-01-26T22:45:31Z")

</div>

Again thanks Paul.

Alas the `template` node is also one of those nodes into which I have had little use/need/experience so it was overlooked.

I shall try to store that in the not too reliable memory for future reference.

---

<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:** [26 January 2020 23:04 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/14 "2020-01-26T23:04:50Z")

</div>

> [@Trying\_to\_learn](#):
>
> What if the incoming message has nothing to do with time?

Urm, well it is a node for translating date/time. If you feed it something that cannot be understood as a date/time value, it can't do anything useful - so it doesn't!

I have thought about making it use the current date/time if it gets something it can't understand but that is fraught with issues. It would mean that, if someone gave it something that they _thought_ was a date but had an error, they would still get a valid output which could be pretty disastrous. Garbage in should always result in an error. Or, in this case, a warning and a blank output so that you can check for that in your flows if you like.

---

<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:** [26 January 2020 23:08 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/15 "2020-01-26T23:08:52Z")

</div>

> [@TotallyInformation](#):
>
> I have thought about making it use the current date/time if it gets something it can't understand but that is fraught with issues.

And I **fully understand that**. I just made the subflow (which is a node in the bigger picture) to create an output so if the _user_ wants to timestamp a message as it passes they can, easily.

I agree @Paul-Reed 's node would (probably) have been easier to implement, but as you can see I just posted that my scope hasn't covered the `template` node either so that is two unknown nodes used and why I didn't.

Anyway, thanks again.

I think we'd better call it a day on that. I seem to have this ability to drag things on way past their "best before" date.  
(pun kind of intended.)

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [27 January 2020 21:48 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/16 "2020-01-27T21:48:18Z")

</div>

@Trying_to_learn, to add a timestamp to a message I used to use the [thethingbox-node-timestamp](https://flows.nodered.org/node/thethingbox-node-timestamp) node. Unfortunately, it gives only a text string in ISO-8601 format, not a unix timestamp. Since the development of the `change` node I have been using that instead. Configured as shown, it gives both the unix time and a formatted string.

 ![timestamp](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc5fd8aa3ed5af08a1cfaef3a5f67012995b8f92.png)

---

<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:** [27 January 2020 23:03 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/17 "2020-01-27T23:03:19Z")

</div>

Yeah, thanks.

But I think @Paul-Reed 's mentioning of `simpletime` node is just as good if not better as it offers more options than just the ISO formatted time stamp.

[Simpletime](https://flows.nodered.org/node/node-red-contrib-simpletime)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 January 2020 23:46 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/18 "2020-01-27T23:46:03Z")

</div>

At the cost of having to install a non core node.  
(Hardly a big problem, but just a little less transportable across systems)

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [28 January 2020 22:49 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/19 "2020-01-28T22:49:13Z")

</div>

if you take a non existing flow.xyz as input in moment it creates a time stamp that you assign to topic output. (formated as you like) A msg.payload from a node before moment will be pased through moment. As a result you get an object with time in topic and your payload.

---

<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:** [6 March 2020 23:38 UTC](https://discourse.nodered.org/t/maybe-a-handy-time-stamp-subflow/20830/20 "2020-03-06T23:38:42Z")

</div>

I've just found a bug in the flow/node/sub-flow.

Nothing too spectacular, but it can be annoying with date formats.

This isn't the best way to _roll out_ the upgrade, but I remember having problems with sub-flows and the `in` and `out` nodes not being taken along with the code.

Simple fix:

Delete the `function` node which gets the time stamp and replace it with a `change` node that sets the payload to `timestamp`.

```auto
[{"id":"5032324b.bcc304","type":"change","z":"5828ee16.d65a","name":"TimeStamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":210,"y":180,"wires":[["ae0623ca.fcb5e"]]}]

```

Then the output format problem I found should be resolved.
