# Get "Day of Year" with NodeRed?

**URL:** <https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028>\
**Category:** General\
**Created:** [16 June 2022 17:44 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028 "2022-06-16T17:44:11Z")\
**Posts on this page:** 6\
**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:** [16 June 2022 17:44 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028/1 "2022-06-16T17:44:12Z")

</div>

Hi there,

to be able to set the limits of my PV according to the expected value of sunshine, I would like to use the DayOfYear as a Trigger.  
Is there a way in Node-Red, comparable to MS SQL --\> select datepart (dayofyear,getdate()) as DayOfYear

that gives me the actual Day of the Year as an integer ?

BR  
Gawan

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [16 June 2022 18:25 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028/2 "2022-06-16T18:25:49Z")

</div>

Try this

```auto
function daysIntoYear(date){
    return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}

```

---

<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:** [16 June 2022 19:28 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028/3 "2022-06-16T19:28:15Z")

</div>

how do I use this code inside a function node ? how can I execute it with the "date" value ?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [17 June 2022 00:03 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028/4 "2022-06-17T00:03:33Z")

</div>

To get todays day number put this into your function node, any trigger will output the day number,

```auto
var date = new Date()
msg.payload = (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0) ) / 24 / 60 / 60 / 1000
return msg;

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [17 June 2022 00:25 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028/5 "2022-06-17T00:25:53Z")

</div>

you could also use the jsonata expression `$moment().dayOfYear()` in a Change node

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/407df3ffa26144f88b264e87f8a3b9f7a65a73b9.png)

**Demo Flow**

```auto
[{"id":"305cc609d8d653e6","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":1360,"wires":[["7d174328c85cee5d"]]},{"id":"7d174328c85cee5d","type":"change","z":"54efb553244c241f","name":"day Of Year","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().dayOfYear()\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1360,"wires":[["1bf74ae613636934"]]},{"id":"1bf74ae613636934","type":"debug","z":"54efb553244c241f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":1360,"wires":[]}]

```

---

<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:** [1 July 2022 18:05 UTC](https://discourse.nodered.org/t/get-day-of-year-with-nodered/64028/7 "2022-07-01T18:05:00Z")

</div>

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