# Christmas lighting date questions

**URL:** <https://discourse.nodered.org/t/christmas-lighting-date-questions/37801>\
**Category:** General\
**Created:** [20 December 2020 15:43 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801 "2020-12-20T15:43:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [20 December 2020 15:43 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801/1 "2020-12-20T15:43:30Z")

</div>

Dear all,  
I fire up my christmas lighting in the morning and in the evening for some time and not during daylight. This works nicely with node-red-contrib-light-scheduler.  
However all this should happen only starting Advent Sunday (this year: 29-NOV) and finish on 06-JAN.  
I want to write a function returning either true/false or providing two exits.

Questions:

1. How do I determine if the **current day is after 06-JAN**?
2. How do I determine the **day of Advent Sunday** (rule: First Sunday after 26-NOV (27 -NOV ... 03-DEC) ???
3. How do I get the **current year** I am in?

I guess that if I can answer 1 then I can answer if I am currently after Advent Sunday.

Any help appreciated! Thanks, Uwe

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [20 December 2020 17:13 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801/2 "2020-12-20T17:13:04Z")

</div>

There are numerous ways to do what you want. Perhaps doing a search for javascript time functions would help. You can get the date with - new Date() - and from there you can get the particulars with - getDate() and getMonth() - w3schools website has a ton of helpful code.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [20 December 2020 17:32 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801/3 "2020-12-20T17:32:55Z")

</div>

Something like this example.

```auto
[{"id":"62276cee.8861cc","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":170,"y":2280,"wires":[["5d0b47d6.d13aa"]]},{"id":"5d0b47d6.d13aa","type":"function","z":"8d22ae29.7df6d","name":"","func":"let date = new Date(); //time now\n// date = new Date(2023,11,6);//manual test date\nlet year = date.getFullYear() - (date.getMonth() === 0 ? 1 : 0); // correct if in january\nlet startDate = new Date( year, 10, (33 - new Date(year,10,26).getDay())); //time start\nlet endDate = new Date(year + 1, 0, 6); // time finish + 1 year\nmsg.payload = (startDate <= date && endDate >= date); // return boolen if date greater than start and less than end\n//msg.dates = [startDate,endDate,date];// log dates for debug\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":2280,"wires":[["84c9d74a.048ca8"]]},{"id":"84c9d74a.048ca8","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":500,"y":2280,"wires":[]}]

```

Edit/ fixed bug in year when in January

---

<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:** [21 December 2020 13:52 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801/4 "2020-12-21T13:52:21Z")

</div>

> [@duaw](#):
>
> I want to write a function returning either true/false or providing two exits.

I used a function node to manage a similar requirement;

```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) {
    return [null, msg];
}
return [msg, null];

```

Regards

---

<div class="post-metadata">

**Author:** ![duaw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/duaw/32/20970_2.png) [@duaw](https://discourse.nodered.org/u/duaw)\
**Post date:** [21 December 2020 15:19 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801/5 "2020-12-21T15:19:03Z")

</div>

Thanks to all!  
I decided to go for

```auto
// isInAdvent time now?
let date = new Date();

let dayOfDate = date.getDate();
let monthOfDate = date.getMonth();
let year = date.getFullYear(); // the year now

// 01-JAN ... 06-JAN
if (monthOfDate == 0 && dayOfDate <= 6)
{
    msg.payload = true;
    return [msg, null];
}

let d = new Date(year, 10, 26); //time on 26th november (10)
let dayofweek = 33 - d.getDay(); // 33 - day of week after 26th

let month = dayofweek < 31 ? 10 : 11; // if less than 31 days month nov else dec
let day = dayofweek < 31 ? dayofweek : dayofweek - 30; //correct days if over 30

// 07-JAN ... day before Advent Start Date
if (monthOfDate <= month && dayOfDate < day)
{
    msg.payload = false;
    return [null, msg];
}
// it's Advent time! 
msg.payload = true;
return [msg, null]

```

This solves my problem. I find a function with two exits ... nice.  
Merry Christmas to all and a happy, healthy New Year 2021!  
Uwe

---

<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:** [19 February 2021 15:19 UTC](https://discourse.nodered.org/t/christmas-lighting-date-questions/37801/6 "2021-02-19T15:19:13Z")

</div>

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