# Add weekdays to date

**URL:** https://discourse.nodered.org/t/add-weekdays-to-date/64123
**Category:** General
**Created:** [19 June 2022 04:48 UTC](https://discourse.nodered.org/t/add-weekdays-to-date/64123 "2022-06-19T04:48:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![animalkingdoms](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/animalkingdoms/32/63461_2.png) [@animalkingdoms](https://discourse.nodered.org/u/animalkingdoms)
#### Post date: [19 June 2022 04:48 UTC](https://discourse.nodered.org/t/add-weekdays-to-date/64123/1 "2022-06-19T04:48:18Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 June 2022 05:51 UTC](https://discourse.nodered.org/t/add-weekdays-to-date/64123/2 "2022-06-19T05:51:14Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![animalkingdoms](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/animalkingdoms/32/63461_2.png) [@animalkingdoms](https://discourse.nodered.org/u/animalkingdoms)
#### Post date: [19 June 2022 06:24 UTC](https://discourse.nodered.org/t/add-weekdays-to-date/64123/3 "2022-06-19T06:24:55Z")

</div>

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?

---

<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: [18 August 2022 06:25 UTC](https://discourse.nodered.org/t/add-weekdays-to-date/64123/4 "2022-08-18T06:25:32Z")

</div>

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