Get DayOfTheYear from moment node?

Is there a way to get the day of the year (like 120 out of 365) from the moment node ?

BR
G

Have a look at the help: Moment.js | Docs
Looks to me that DDD will do the job.

Error: moment is not defined

[{"id":"b6baf30f311ebf45","type":"function","z":"1f12c3c8.4ca37c","name":"","func":"msg.payload = moment().format(\"DDD\");                                // \"2014-09-08T08:02:17-05:00\" (ISO 8601, no fractional seconds)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":60,"wires":[["94d616a4ab53c479"]]},{"id":"a1986fa9bc2b4dce","type":"inject","z":"1f12c3c8.4ca37c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":60,"wires":[["b6baf30f311ebf45"]]},{"id":"94d616a4ab53c479","type":"debug","z":"1f12c3c8.4ca37c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":60,"wires":[]}]

A quick google search shows many ways to do this. Here is an example

[{"id":"b6baf30f311ebf45","type":"function","z":"4e2a697d7291533e","name":"","func":"const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24));\nmsg.payload = dayOfYear(new Date());\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":300,"wires":[["94d616a4ab53c479"]]},{"id":"a1986fa9bc2b4dce","type":"inject","z":"4e2a697d7291533e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":300,"wires":[["b6baf30f311ebf45"]]},{"id":"94d616a4ab53c479","type":"debug","z":"4e2a697d7291533e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":300,"wires":[]}]
1 Like

You have to add moment to the start up tab of the function node.
e.g.

[{"id":"a1986fa9bc2b4dce","type":"inject","z":"65617ffeb779f51c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":1380,"wires":[["b6baf30f311ebf45"]]},{"id":"b6baf30f311ebf45","type":"function","z":"65617ffeb779f51c","name":"","func":"msg.payload = moment().format(\"DDD\");                                // \"2014-09-08T08:02:17-05:00\" (ISO 8601, no fractional seconds)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":400,"y":1380,"wires":[["94d616a4ab53c479"]]},{"id":"94d616a4ab53c479","type":"debug","z":"65617ffeb779f51c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":1380,"wires":[]}]
1 Like

does not work:

image

@E1cid flow works fine for me.

Just a thought... Have you got the moment node installed via the palette manager ?

Is functionExternalModules set to true in your settings.js.
Writing Functions : Node-RED.
Also you can use JSONata in the function node see here

yes

image

Install the node with the palletmanager as Dave suggested.

Fill in the output format of the node like this with the DDD and you are done.

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