# Moment node replacement

**URL:** https://discourse.nodered.org/t/moment-node-replacement/86291
**Category:** General
**Created:** [9 March 2024 04:11 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291 "2024-03-09T04:11:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![SmokinSowbellies](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@SmokinSowbellies](https://discourse.nodered.org/u/SmokinSowbellies)
#### Post date: [9 March 2024 04:11 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/1 "2024-03-09T04:11:28Z")

</div>

I had a moment node to up my google home to cast a weather function I built. The moment node would give me today's date in the format of "Friday March the 8th" and the names of the next 5 days for the forecast. I updated some nodes (probably moment) and it disappeared. The only solution I have found so far is in a function node as follows;

```auto
// $now([picture [, timezone]])

const data = {
    // example1: "[M01]/[D01]/[Y0001] [h#1]:[m01][P]",
    example1: "[FNn] [MNn] the [D1o]",
    example2: "[H01]:[m01]:[s01] [z]",
    example3: "-0600",
};

// Generates a UTC timestamp in ISO 8601 compatible format and returns it as a string
// If the optional picture and timezone parameters are supplied,
// then the current timestamp is formatted as described by the $fromMillis() function
const expression = [
    jsonata("$now()"),
    jsonata("$now(example1, example3)"),
    jsonata("$now(example2, example3)"),
];

msg.payload = [
    // expression[0].evaluate(data),
    expression[1].evaluate(data),
    // expression[2].evaluate(data),
];
return msg;

```

This is only for today's date, and I don't know how to get this into the global context, or how to add days to this.  
Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [9 March 2024 05:03 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/2 "2024-03-09T05:03:59Z")

</div>

May not be a perfect solution, but give this a try

```auto
[{"id":"35acf98972fa0867","type":"inject","z":"f31cf702402bd743","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":200,"wires":[["1e17cc276a065821"]]},{"id":"1e17cc276a065821","type":"change","z":"f31cf702402bd743","name":"","rules":[{"t":"set","p":"payload1","pt":"msg","to":"payload+8.64e+7","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"$fromMillis(payload,'[FNn], [MNn] the [D1o]')","tot":"jsonata"},{"t":"set","p":"date0","pt":"global","to":"payload","tot":"msg"},{"t":"set","p":"payload1","pt":"msg","to":"$fromMillis(payload1,'[FNn], [MNn] the [D1o]')","tot":"jsonata"},{"t":"set","p":"date1","pt":"global","to":"payload1","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":200,"wires":[["c1857e9305351649"]]},{"id":"c1857e9305351649","type":"debug","z":"f31cf702402bd743","name":"debug 2535","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload &\"-\"& payload1","statusType":"jsonata","x":700,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![SmokinSowbellies](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@SmokinSowbellies](https://discourse.nodered.org/u/SmokinSowbellies)
#### Post date: [9 March 2024 05:55 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/3 "2024-03-09T05:55:58Z")

</div>

It is far prettier than what I had going. Thank you

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [9 March 2024 08:33 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/4 "2024-03-09T08:33:26Z")

</div>

You do know that you can use $moment() in a change node.  
If you do not give the moment an argument it uses the time now.  
here is an example [Moment.js | Docs](https://momentjs.com/docs/?/displaying/format/#/displaying/format/)

```auto
[{"id":"35acf98972fa0867","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":440,"y":7680,"wires":[["1e17cc276a065821"]]},{"id":"1e17cc276a065821","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.payload).tz(\"London/Europe\").format(\"dddd MMMM [the] Do\")","tot":"jsonata"},{"t":"set","p":"payload1","pt":"msg","to":"$moment().add(1,\"day\").tz(\"London/Europe\").format(\"dddd, MMMM [the] Do\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":7680,"wires":[["c1857e9305351649"]]},{"id":"c1857e9305351649","type":"debug","z":"d1395164b4eec73e","name":"debug 2535","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload &\"-\"& payload1","statusType":"jsonata","x":830,"y":7680,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)
#### Post date: [9 March 2024 09:03 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/5 "2024-03-09T09:03:17Z")

</div>

**New learnings**

add 1 day.  
[the] - literal string in square bracket

thanks.

---

<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: [9 March 2024 13:00 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/6 "2024-03-09T13:00:22Z")

</div>

Check out the INTL library which is native to Node.js (and modern browsers). With it, you shouldn't need Moment (which is no longer being developed anyway) for things like this.

Also, if a node has stopped working, check the Node-RED logs for errors, it may be fixable.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [23 March 2024 13:01 UTC](https://discourse.nodered.org/t/moment-node-replacement/86291/7 "2024-03-23T13:01:20Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
