# Ordinal day of Year needed

**URL:** https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886
**Category:** General
**Created:** [4 May 2025 20:09 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886 "2025-05-04T20:09:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![larkingabout](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/larkingabout/32/100581_2.png) [@larkingabout](https://discourse.nodered.org/u/larkingabout)
#### Post date: [4 May 2025 20:09 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886/1 "2025-05-04T20:09:52Z")

</div>

Im running an older V0.19.5 Node Red instance on a PI-0-W with the Stretch version of the pi-os,  
I know i should have been updating, but its been running flawlessly mostly handling mqtt for basic automation.

Its been decades since i approached the code-face for anything but a lil fun and distraction,  
and re-flashing a new up to date system sd card and reconfiguring my basic set up with a later version of node red which will enable pallate updates is on the cards, but meanwhile i would like to solve this.

I needed to make a slight modification to the watering schedule for the patio tomatoes as to not drown them while growing, and i decided to water on alternative days so  
Mod(ordinalDayofyear/2) is my key condition.

I was expecting to find a date object with ordinal day of year as a basic property, its not??

Im currently working with mod(day of month/2) but with a 29 or 31 day month I will get numerous double watering days per anum, now granted not too crucial when it comes to tomatoes but still, its not good enough.

I can see how to sum the current day of month with the day count of earlier months in the current year, but that seems a bit naff too

IM using the inject node timestamp as my date source. this starts the flow at a regular interval,  
my function node returns the odd even day evaluation of mod(dayofmonth/2) to a switch node that passes onto a mqtt-create change node to start watering for X minutes..

Thanks in advance if you can advise..

---

<div class="post-metadata">

### Author: ![larkingabout](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/larkingabout/32/100581_2.png) [@larkingabout](https://discourse.nodered.org/u/larkingabout)
#### Post date: [4 May 2025 21:03 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886/2 "2025-05-04T21:03:28Z")

</div>

and sorted..

```auto
const aDay = 1000 * 60 * 60 * 24;
var dt = new Date(msg.payload);
var st = new Date();
var DayOfYear = 0;

  st.setDate(1);
  st.setMonth(0);
  st.setHours(0);
  st.setMinutes(0);
  st.setSeconds(0);
  st.setMilliseconds(0);
 
 
 
 DayOfYear = Math.trunc((dt.getTime() - st.getTime())/ aDay) + 1;
 msg.payload = DayOfYear;
 return msg;

```

Cheers..

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [4 May 2025 21:15 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886/3 "2025-05-04T21:15:47Z")

</div>

I think you should be able to use this short version

```auto
const dayOfYear = (date) =>
  Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);

const result = dayOfYear(msg.payload); 

```

---

<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: [4 May 2025 21:36 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886/4 "2025-05-04T21:36:54Z")

</div>

Or alternate days output 0 or 1, from an input of epoch timestamp.

```auto
msg.payload = Math.floor(msg.payload / 86400000) % 2;

```

---

<div class="post-metadata">

### Author: ![larkingabout](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/larkingabout/32/100581_2.png) [@larkingabout](https://discourse.nodered.org/u/larkingabout)
#### Post date: [4 May 2025 23:07 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886/5 "2025-05-04T23:07:03Z")

</div>

Thanks ,both are much more elegant than my plod though. Easy when your shown 😉

- It seems the install on the pi-0-w isnt the headache it was either..  
arm6 and 7 core differences iirc no longer an issue phew!.

---

<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: [2 August 2025 23:07 UTC](https://discourse.nodered.org/t/ordinal-day-of-year-needed/96886/6 "2025-08-02T23:07:08Z")

</div>

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