Add weekdays to date

I am trying to add x number of business days to a date now() and have tried several methods without any success.
Business days meaning Monday to Friday
node-red-contrib-calc-next-work-days 0.0.6 looked promising, but it is counting every day including the weekends after many attempts and testing on two versions of nodered
It can exclude predefined 'holiday' days correctly.

I could do the method of adding all weekend days into the holiday fields and manage it that way, but it seems overly inefficient and a messy work around.

How would you add x number of weekdays/business days to a date in an efficient way that needs no future maintenance?

You can use a function node instead, define the holidays and check for weekends:

const holidays = [
  '2022-01-03', '2022-04-15', '2022-04-18', '2022-05-02', '2022-05-30', '2022-08-29', '2022-12-26', '2022-12-27',
  '2023-01-02', '2023-04-07', '2023-04-10', '2023-05-01', '2023-05-29', '2023-08-28', '2023-12-25', '2023-12-26']

function nextBusinessDay(date){              
   date.setDate(date.getDate()+1);
   if(date.getDay() % 6 == 0 || holidays.includes(date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate())){
      return nextBusinessDay(date)
   }else 
   return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()
}

msg.payload = nextBusinessDay(new Date())

return msg;

result: 2022-6-20

1 Like

Thanks - That still seems to count the weekend though.
If i modify it to be
date.setDate(date.getDate()+10); to add 10 business days it returns 29 June. where it should be 01 July if it was to not count saturday and sunday.

Unless im modifying that code wrong?

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