Get "Day of Year" with NodeRed?

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

Try this

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

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

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

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;

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

image

Demo Flow

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

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