Ordinal day of Year needed

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

and sorted..

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

1 Like

I think you should be able to use this short version

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


const result = dayOfYear(msg.payload); 

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

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

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

  • 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!.