# Formatting times & dates with moment.js examples

**URL:** <https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553>\
**Category:** Share Your Projects\
**Created:** [5 July 2020 13:21 UTC](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553 "2020-07-05T13:21:49Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [5 July 2020 13:21 UTC](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553/1 "2020-07-05T13:21:49Z")

</div>

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`.

![moment](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/beeb5deb835d7af762085885a0f64ee279bcfc03.jpeg)

```auto
[{"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/](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;  
 ![moment2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a65576feb31dc288755168c9b65c22d599fb8298.jpeg)  
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!!

---

<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:** [5 July 2020 15:17 UTC](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553/2 "2020-07-05T15:17:11Z")

</div>

You can also use it to parse “strings” into dates using a template

---

<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:** [5 July 2020 15:48 UTC](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553/3 "2020-07-05T15:48:42Z")

</div>

> [@dceejay](#):
>
> parse “strings” into dates using a template

Do you mean like `moment().format("MMM Do YYYY");` resulting in `Jul 5th 2020`?

---

<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:** [5 July 2020 16:04 UTC](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553/4 "2020-07-05T16:04:34Z")

</div>

Opposite way round like [https://momentjs.com/docs/#/parsing/string-format/](https://momentjs.com/docs/#/parsing/string-format/)

---

<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:** [3 September 2020 13:21 UTC](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553/5 "2020-09-03T13:21:50Z")

</div>

This topic was automatically closed after 60 days. New replies are no longer allowed.
