`moment` node, times and date stamps

Yeah, I'm still not getting it.

I've looked back on this thread. Some help.
Moment node
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
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.

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

[{"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?

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')
1 Like

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

The flow I posted compared to what you say:

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.

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.

1 Like

Even simpler is to pick the timestamp option :slight_smile:


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