Moment node replacement

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;

// $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.

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

[{"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":[]}]
1 Like

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

1 Like

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

[{"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":[]}]
1 Like

New learnings

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

thanks.

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.

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