# Is Today() Between Two Dates?

**URL:** https://discourse.nodered.org/t/is-today-between-two-dates/37012
**Category:** General
**Created:** [4 December 2020 15:37 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012 "2020-12-04T15:37:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rcblackwell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rcblackwell/32/30964_2.png) [@rcblackwell](https://discourse.nodered.org/u/rcblackwell)
#### Post date: [4 December 2020 15:37 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/1 "2020-12-04T15:37:35Z")

</div>

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?

```auto
//####################################################
//# 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

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [4 December 2020 16:06 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/2 "2020-12-04T16:06:27Z")

</div>

Welcome to the forum !

I would check this using a JSONata expression:

- [https://try.jsonata.org/tehsxPHQI](https://try.jsonata.org/tehsxPHQI)

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

```

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

---

<div class="post-metadata">

### Author: ![rcblackwell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rcblackwell/32/30964_2.png) [@rcblackwell](https://discourse.nodered.org/u/rcblackwell)
#### Post date: [4 December 2020 16:28 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/3 "2020-12-04T16:28:59Z")

</div>

> [@janvda](#):
>
> I would check this using a JSONata expression:

Thank you for the warm welcome!

```auto
( $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?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [4 December 2020 16:41 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/4 "2020-12-04T16:41:48Z")

</div>

> [@rcblackwell](#):
>
> ```auto
> 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];
> }
> 
> ```

> [@rcblackwell](#):
>
> I think there may be a better way: less variables to test at the IF statement

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

```auto
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](https://stackoverflow.com/questions/19041946/why-faster-than-in-javascript) provide a miniscule (but practically unnoticeable) improvement._

---

<div class="post-metadata">

### Author: ![rcblackwell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rcblackwell/32/30964_2.png) [@rcblackwell](https://discourse.nodered.org/u/rcblackwell)
#### Post date: [4 December 2020 19:43 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/5 "2020-12-04T19:43:26Z")

</div>

> [@Steve-Mcl](#):
>
> What is the issue with the amount of tests?

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

---

<div class="post-metadata">

### Author: ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)
#### Post date: [4 December 2020 20:25 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/6 "2020-12-04T20:25:03Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [2 February 2021 20:25 UTC](https://discourse.nodered.org/t/is-today-between-two-dates/37012/7 "2021-02-02T20:25:06Z")

</div>

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