Christmas lighting date questions

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

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.

Something like this example.

[{"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

I used a function node to manage a similar requirement;

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

Thanks to all!
I decided to go for

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

1 Like

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