# Get DayOfTheYear from moment node?

**URL:** <https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009>\
**Category:** General\
**Tags:** function-node\
**Created:** [28 March 2023 08:40 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009 "2023-03-28T08:40:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [28 March 2023 08:40 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/1 "2023-03-28T08:40:30Z")

</div>

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

BR  
G

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [28 March 2023 08:45 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/2 "2023-03-28T08:45:42Z")

</div>

Have a look at the help: [Moment.js | Docs](https://momentjs.com/docs/#/displaying/format/)  
Looks to me that **DDD** will do the job.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [28 March 2023 08:59 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/3 "2023-03-28T08:59:11Z")

</div>

Error: moment is not defined

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 March 2023 09:43 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/4 "2023-03-28T09:43:09Z")

</div>

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

```auto
[{"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":[]}]

```

---

<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:** [28 March 2023 11:51 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/5 "2023-03-28T11:51:57Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [28 March 2023 12:38 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/6 "2023-03-28T12:38:30Z")

</div>

does not work:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/943e70f7629d0edf36b9bd7e4ee9fefb1599075e.png)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [28 March 2023 12:46 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/7 "2023-03-28T12:46:00Z")

</div>

@E1cid flow works fine for me.

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

 ![moment_palette](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/1895a952fe048b2a1ea0a1fcd501455cc88851e5.png)

---

<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:** [28 March 2023 12:52 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/8 "2023-03-28T12:52:12Z")

</div>

Is functionExternalModules set to true in your settings.js.  
[Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option).  
Also you can use JSONata in the function node see [here](https://discourse.nodered.org/t/using-moment-js-in-a-function-node-nr-3-01-2023/76916/7)

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [28 March 2023 12:57 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/9 "2023-03-28T12:57:56Z")

</div>

yes

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/483d5d5ba943efe525b917c958f214a76a860d2a.png)

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [28 March 2023 14:15 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/10 "2023-03-28T14:15:53Z")

</div>

Install the node with the palletmanager as Dave suggested.

Fill in the _output forma_t of the node like this with the **DDD** and you are done.

 ![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/79eb7131003017360574f06bde197a43577d20a5.png)

---

<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:** [11 April 2023 14:16 UTC](https://discourse.nodered.org/t/get-dayoftheyear-from-moment-node/77009/11 "2023-04-11T14:16:01Z")

</div>

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