Is Today() Between Two Dates?

I'm new to Node-Red; struggling to learn code that may be applied in a (Function) node. I've written the following which tests to see if todays date falls between November 15th of the current year and January 31 of the following year. It's working okay however I think there may be a better way: less variables to test at the IF statement. Something like IF TODAY() is between START_DATE and END_DATE where November 15, CURRENT_YEAR and January 31, NEXT_YEAR are represented respectively by the above noted variables.

I've tried several ways of writing the statement but each fails to give appropriate results. Would someone be kind enough to a) write the code or b) provide a few hints that would help me out?

``````//####################################################
//# Operate switch if date is between Nov 15 and Jan 15
//####################################################

let today = new Date();
let dd = today.getDate();
let mm = today.getMonth()+1; //January is 0!
if ((mm == 11 && dd >= 15) || mm == 12 || (mm == 1 && dd <= 31)) {
return [null, msg];
} else {
return [msg, null];
}
``````

Regards, Robert

Welcome to the forum !

I would check this using a JSONata expression:

``````( \$toMillis(\$now()) >= \$toMillis('2020-11-15') ) and
( \$toMillis(\$now()) < \$toMillis('2021-02-01') )
``````

You can use JSONata expressions in change node or switch node.

Thank you for the warm welcome!

``````( \$toMillis(\$now()) >= \$toMillis('2020-11-15') ) and
( \$toMillis(\$now()) < \$toMillis('2021-02-01') )
``````

The expression needs to account for a change in years so it doesn't need to be updated every year. How do I evaluate such change?

What is the issue with the amount of tests? what you have written is fine (maybe a very small improvement is possible) but it is readable, concise, maintainable and probably close to the (operationally) fastest solution (no external libs / JSONata etc).

Small "improvement" below...

``````let today = new Date();
let dd = today.getDate();
let mm = today.getMonth()+1; //January is 0!
if ((mm === 11 && dd >= 15) || mm === 12 || mm === 1) {
return [null, msg];
}
return [msg, null];
``````

Use of `===` might provide a miniscule (but practically unnoticeable) improvement.

1 Like

Hi @Steve-Mcl,

I didn't think there was anything wrong with what was written. I'm just looking for alternatives so that as I expand programming, I'm aware of possible options. Thank you for pointing out the minor adjustments, especially the need to exclude the date test in January.

Regards

There are always more solutions to a problem.
Solution from Steve and janvda works perfect, this is how I should do it.

``````var startdate = new Date("2020-11-15");
var enddate = new Date("2021-02-01");
var today = new Date();

if (today  > startdate  && today < enddate) {
return [null, msg];
} else {
return [msg, null];
}
``````

You can easily replace the "2020-11-15" with a payload.

1 Like

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