Is today between two relative dates?

Hey Guys!
I'm tryna check with a node or sth. if today is e.g. between April, the 26th and October the 10th or not.
Idea is to switch on a pump only on summer or sth. Therefore it should be relative and not absolute. What I mean is: whether if its 2022 or 2025, it should only check, if today is between these dates in terms of day and months.

Any clue for me?
Regards

Since the relative dates don't cross a year boundary, probably the simplest way is to dynamically create the start/end dates using the current year. Then you can compare them directly.

Something like (untried)

const thisYear = (new Date()).year()
const startDate = new Date(`${thisYear}-04-26`)
const endDate = new Date(`${thisYear}-10-10`)
const now = new Date
if ( now >= startDate && now <= endDate ) {
    // ...
}

THANK YOU for your time. Unfortunately I'm not really a Coder. But I always try to learn and try to understand those code snippets.

I understand that the function set the variables thisYear / startDate / endDate and now and then compares those dates. Is there a way to deconstruct the code and to iterate the solution for me?

If i put your code into a function node, i get TypeError: (intermediate value).year is not a function.

MDN is a great site for learning and reference for JavaScript.

Date.prototype.getFullYear() - JavaScript | MDN (mozilla.org)

Well, there's not much to it really.

// Gets this year by creating a new date object for now and extracting just the year
const thisYear = (new Date()).getFullYear()
// Constructs a new date object in ISO form '2022-04-06' and saves as the start date
const startDate = new Date(`${thisYear}-04-26`)
// End date of course
const endDate = new Date(`${thisYear}-10-10`)
// And finally the date/time right now (when the function runs)
const now = new Date
// JS Date objects are long numbers and can be directly compared.
if ( now >= startDate && now <= endDate ) {
    // ... do whatever you want in here, for example:
    msg.payload = true
}
// Send the message onward to the next node
return msg
1 Like

Thanks it workls like charm!

How can i achieve that true only takes output 0 and false goes to output 1. Had adapted the code to this:

// Gets this year by creating a new date object for now and extracting just the year
const thisYear = (new Date()).getFullYear()
// Constructs a new date object in ISO form '2022-04-06' and saves as the start date
const startDate = new Date(`${thisYear}-06-26`)
// End date of course
const endDate = new Date(`${thisYear}-10-10`)
// And finally the date/time right now (when the function runs)
const now = new Date
// JS Date objects are long numbers and can be directly compared.
if ( now >= startDate && now <= endDate ) {
    // ... do whatever you want in here, for example:
    msg.payload = true
}
else {
    msg.payload = false
}

// Send the message onward to the next node
return msg

You mean that you want two different output ports on the function node?

Set the output ports number to 2 and then change the return so that it is an array of objects.

if ( now >= startDate && now <= endDate ) {
    // ... do whatever you want in here, for example:
    msg.payload = true
    return [msg, null] // assumes you want the true path to be on port 1
}
// No need for an else
msg.payload = false
return [null, msg]
1 Like

damn i was so close!

Thank you again!!

1 Like

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