I have a flow I use to set an input select based on the current date. It is working fine, but I have to define my start and end date ranges manually. That works, until I forget to update the year for each of them. I’m trying to figure out how to automatically adjust the year in the start and end dates, but am not having much luck.
My function nodes look like this.
Any idea how I can have the startdate and enddate variables use the current year, instead of having to update them year over year? Any help would be much appreciated!
var startdate = new Date("2024-06-25");
var enddate = new Date("2024-07-10");
var today = new Date();
if (today > startdate && today < enddate) {
msg.payload = "Fourth of July";
return msg;
} else {
msg.payload = "None";
return msg;
}
Thanks, I was looking along those lines. For anyone that might come across this thread, here is how I ended up making the function nodes work.
var year = new Date().getFullYear()
var startdate = new Date(year + "-" + "06-25");
var enddate = new Date(year + "-" + "07-10");
var today = new Date();
if (today > startdate && today < enddate) {
msg.payload = "Fourth of July";
return msg;
} else {
msg.payload = "None";
return msg;
}
That link I posted is a pretty comprehensive javascript guide, pretty much everything there will work in a function node. It’s recently been brought to my attention that using var should be avoid. Instead use const and let.
Your particular function I would use const to declare the variable, since once it’s created it’s value does not change later in the function. If it did need to change within the function the use let.