# \`moment\` node, times and date stamps

**URL:** <https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826>\
**Category:** General\
**Created:** [25 January 2020 00:59 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826 "2020-01-25T00:59:56Z")\
**Posts on this page:** 7\
**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 00:59 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/1 "2020-01-25T00:59:56Z")

</div>

Yeah, I'm still not getting it.

I've looked back on this thread. Some help.  
[Moment node](https://discourse.nodered.org/t/using-moment-node-not-quite-there/1962)  
But I am (_typically_) missing something.

Adding slight insult to injury it is 11:49 here and I am not really wanting to wait 1:10 to test what I am doing.

I know, I know I know. ZULU time is the preferred time format.  
When you are 10/11 hours off ZULU and your brain isn't too good most of the time, it is annoying when (for instance) I am looking at a log at 07:20 local and I am seeing an entry (the last one) with yesterday's date.

It really throws me.

I'd just prefer to stamp things with LOCAL time.

I also know that I've asked this before and have probably got answers.  
Finding them, the code which was modified and rolling it out is/are another story/ies.

Yes: **my** problem. I get it.

But there must be a way.

I get how the `moment` node works.

I can get the date time coming out the way I like it and it looks good.  
Alas being 11:51 (now) local, am still not 100% sure it is what I want.  
The appendage of AM/PM is not welcome on time stamps.

I am wanting `YY MM DD HH mm ss` output. As is specified in `moment`.

To get local time I have got/use `var d = new Date().toLocaleString();` (where `d` stores the value) All good.  
Well, kind of.

If I want to use the date/time as a file name I make it this:  
`var d = new Date().toLocaleString().replace(/:/g,"");` which strips the `:` from the format.

I seem to remember that this pesky `AM` and `PM` are sneaking in.  
So with the code I posted, how do I force it to be 24 format? (`HH` rather than `hh`)

I'm now looking at this site:  
[Date/time format](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString)  
But it is _Greek_ to me.  
I'm not getting the syntax.

I'm also wanting two digit dates/times if they are single.  
It is mentioned but explained how to get it.

---

<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 02:01 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/2 "2020-01-25T02:01:37Z")

</div>

This is the best I can do to get what I want.

```auto
[{"id":"808b65ed.85f658","type":"inject","z":"184dc884.7aba5f","name":"Read","topic":"","payload":" ","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":110,"y":3190,"wires":[["cb937d49.26d168"]]},{"id":"cb937d49.26d168","type":"function","z":"184dc884.7aba5f","name":"Time Stamp","func":"var d = new Date().toLocaleString();\nmsg.payload = d;\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":3190,"wires":[["a302bbd.8d904c8"]]},{"id":"a302bbd.8d904c8","type":"moment","z":"184dc884.7aba5f","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":470,"y":3190,"wires":[["c1bdb13c.5d664"]]},{"id":"8a98bbc3.58eb18","type":"debug","z":"184dc884.7aba5f","name":"24","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":770,"y":3190,"wires":[]},{"id":"c1bdb13c.5d664","type":"string","z":"184dc884.7aba5f","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":640,"y":3190,"wires":[["8a98bbc3.58eb18"]]}]

```

Can it be better optimised?

---

<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 10:56 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/3 "2020-01-25T10:56:11Z")

</div>

Get rid of the `function` and `string` nodes.  
Change the `moment` node so:

- `Input from` is a `timestamp` and
- `Output Format` is 'YYYY-DD-MMHHmmss' (but you might want 'YYYY-DD-MM-HHmmss')

---

<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:58 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/4 "2020-01-25T21:58:01Z")

</div>

Thanks Paul, but I think you are missing an important point.

The _flow_ I posted compared to what you say:

> [@zenofmud](#):
>
> Get rid of the `function`

I can't. It is not a given the incoming message is a timestamp.

~~Also I have _improved_ it a bit. Did you see my _share your projects_ post?~~

Ok, you have I just saw.

I'll have to read the other posts on that.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 January 2020 22:43 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/5 "2020-01-25T22:43:49Z")

</div>

A different way to create a timestamp as input for `moment` from within a flow is by using change node. Simply set the property to `$now()` with the input type set to JSONata.

---

<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:** [25 January 2020 22:47 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/6 "2020-01-25T22:47:13Z")

</div>

> [@afelix](#):
>
> Simply set the property to `$now()` with the input type set to JSONata.

Even simpler is to pick the timestamp option 🙂

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 January 2020 23:04 UTC](https://discourse.nodered.org/t/moment-node-times-and-date-stamps/20826/7 "2020-01-25T23:04:00Z")

</div>

On the change node? I guess I’ve to look more closely next time I’ve the editor open. Thanks for the tip. 🙂 Although now I vaguely remember there being something with timestamp as option for TypedInputs last time I used it.....
