Calculate Date of Thanksgiving Holiday

Hi all,

I'm using Node Red in Home Assistant to automate holiday lights around my house. I'm currently hard coding the actual date of Thanksgiving, but would like to make it automatically determine the date. I've seen this in a search result, but have no clue how to use it in a function node. Here is my current node, I go in and edit the enddate variable, but that is the part I'd like to calculate.

`var year = new Date().getFullYear() 
var startdate = new Date(year + "-" + "11-02");
var enddate = new Date(year + "-" + "11-29");
var today = new Date();

if (msg.payload == "Fourth of July") {
   msg.payload = "Fourth of July";
   return msg;
} else if (msg.payload == "Halloween") {
    msg.payload = "Halloween";
    return msg;
} else if (today  > startdate  && today < enddate) {
    msg.payload = "Thanksgiving";
    return msg;
} else {
    msg.payload = "None";
    return msg;
}`

Here is what I found on how to calculate the date.

thanksgivingDayUSA = (year = (new Date).getFullYear()) ->
  first = new Date year, 10, 1
  day_of_week = first.getDay()
  22 + (11 - day_of_week) % 7

Any help would be much appreciated.

I simply asked ChatGPT - I've not tested this BTW , so let me know if it works. :slight_smile:

Put the code into a function node and change the last line to:

msg.payload = getNextThanksgiving()
return msg
1 Like

Oh man, I didn't even think of that. The code came back with the date being a day off. I asked ChatGPT to fix it, and it did. ChatGPT can't run JavaScript, so the day of the week was off between JavaScript and Python. Thank you for the help and here is the final code snippet if anyone is interested.

function getNextThanksgiving() {
  const now = new Date();
  let year = now.getFullYear();
  
  // November 1st of the current year
  const novemberFirst = new Date(year, 10, 1); // 10 is November (0-indexed)
  
  // Find the day of the week for November 1st
  const dayOfWeek = novemberFirst.getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday
  
  // Offset to the first Thursday in November
  const firstThursdayOffset = (11 - dayOfWeek) % 7; // Thursday = 4 (0-indexed in JavaScript)
  const firstThursday = new Date(year, 10, 1 + firstThursdayOffset);
  
  // Calculate the date of the fourth Thursday in November
  const fourthThursday = new Date(firstThursday);
  fourthThursday.setDate(firstThursday.getDate() + 21); // Add 3 weeks (21 days)

  // If today is after this year's Thanksgiving, calculate for next year
  if (now > fourthThursday) {
    year += 1;
    return getNextThanksgivingForYear(year);
  }
  
  return fourthThursday;
}

function getNextThanksgivingForYear(year) {
  const novemberFirst = new Date(year, 10, 1); // November 1st
  const dayOfWeek = novemberFirst.getDay();
  const firstThursdayOffset = (11 - dayOfWeek) % 7; // Thursday = 4
  const firstThursday = new Date(year, 10, 1 + firstThursdayOffset);
  const fourthThursday = new Date(firstThursday);
  fourthThursday.setDate(firstThursday.getDate() + 21); // Add 3 weeks
  return fourthThursday;
}

msg.payload = getNextThanksgiving()
return msg
1 Like

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