Since node-RED v1.1.0 support for the moment.js date/time library has been added, currently only available for jsonata expressions, but hopefully in time to also be added to the function node.
I've included a few examples below, of how Moment can be used to provide date/time functions.
Please feel free to add others
This is the basic format, which creates a realtime UTC timestamp similar to Sun Jul 05 2020 13:22:30 GMT+0000
.
[{"id":"54c46ebe.3b623","type":"change","z":"a444a9ff.e7a408","name":"Change node","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":2890,"wires":[["2c896b1f.98a694"]]},{"id":"2c896b1f.98a694","type":"debug","z":"a444a9ff.e7a408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":2890,"wires":[]},{"id":"7991fc04.3ff914","type":"inject","z":"a444a9ff.e7a408","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":210,"y":2890,"wires":[["54c46ebe.3b623"]]}]
A timezone offset can then be added to the change node to produce the time/date in your particular locale;
$moment().tz("Europe/London")
This will produce an output similar to Sun Jul 05 2020 14:18:02 GMT+0100
.
To change the output format of the date/time, further code can be added, for example;
$moment().tz("Europe/London").format('l')
will output 7/5/2020
whilst $moment().tz("Europe/London").format('lll')
will output Jul 5, 2020 2:18 PM
A full list of the options can be seen at https://momentjs.com/
Instead of Moment creating the date stamp, you can also enter a datestamp of your choosing, such as 2020-07-01T13:00:52.345Z
or 1593608452000
in this format;
$moment(2020-07-01T13:00:52.345Z).tz("Europe/London").format('lll')
which results in July 1, 2020 2:00 PM
.
In practice, the timestamp that you wish to use would be added via the flow payload;
using $moment(payload).tz("Europe/London").format('LLL')
in the change node.
To convert a UTC timestamp to a Unix epoch timestamp, for example to convert 2020-07-01T13:00:52.345Z
to 1593608452000
, use the code $moment(payload).unix()*1000
There are countless other formats and uses for Moment.js, this is just a taster!!