# Is today between two relative dates?

**URL:** <https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025>\
**Category:** General\
**Created:** [24 May 2022 20:29 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025 "2022-05-24T20:29:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [24 May 2022 20:29 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/1 "2022-05-24T20:29:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [24 May 2022 20:39 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/2 "2022-05-24T20:39:25Z")

</div>

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)

```auto
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 ) {
    // ...
}

```

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [25 May 2022 07:29 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/3 "2022-05-25T07:29:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 May 2022 08:10 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/4 "2022-05-25T08:10:35Z")

</div>

> [@terminus](#):
>
> 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)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear)

> [@terminus](#):
>
> Is there a way to deconstruct the code and to iterate the solution for me

Well, there's not much to it really.

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

```

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [25 May 2022 11:25 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/5 "2022-05-25T11:25:30Z")

</div>

> [@TotallyInformation](#):
>
> ` now and extracting just the year`

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:

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

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 May 2022 11:29 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/6 "2022-05-25T11:29:20Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![terminus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/terminus/32/38378_2.png) [@terminus](https://discourse.nodered.org/u/terminus)\
**Post date:** [25 May 2022 11:46 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/7 "2022-05-25T11:46:58Z")

</div>

damn i was so close!

Thank you again!!

---

<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:** [8 June 2022 11:47 UTC](https://discourse.nodered.org/t/is-today-between-two-relative-dates/63025/8 "2022-06-08T11:47:07Z")

</div>

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