# Javascript to calculate Work days

**URL:** <https://community.glideapps.com/t/javascript-to-calculate-work-days/63838>\
**Category:** Ask for Help\
**Created:** [July 12, 2023, 10:21pm UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838 "2023-07-12T22:21:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thais\_Fontes](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thais_fontes/32/61251_2.png) [@Thais\_Fontes](https://community.glideapps.com/u/Thais_Fontes)\
**Post date:** [July 12, 2023, 10:21pm UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/1 "2023-07-12T22:21:27Z")

</div>

Hello,  
I am not good with Javascript so any help is welcome.

I’m trying to calculate what is the next workday after X days by using the below code, but for some reason it is not returning anything.  
The code is returning the good value on JSFiddle tho.

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/c/b/cbf5244d9ea892518ec9da43bb847c1f84e44740.png)

```auto
const holidays = {p3};
const startDate = new Date(p1);
const numDays = p2;
const futureDate = addWorkingDays(startDate, numDays);

function isHoliday(date) {
  const dateString = date.toISOString().slice(0, 10);
  return dateString in holidays;
}

function addWorkingDays(startDate, numDays) {
  let currentDate = new Date(startDate.getTime());
  let daysAdded = 0;

  while (daysAdded < numDays) {
    currentDate.setDate(currentDate.getDate() + 1);

    // Check if the current day is a weekend or holiday
    if (currentDate.getDay() === 0 || currentDate.getDay() === 6 || isHoliday(currentDate)) {
      continue; // Skip weekends and holidays
    }

    // If it's a working day, increment the counter
    daysAdded++;
  }

  return currentDate;
}

```

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [July 13, 2023, 12:47am UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/2 "2023-07-13T00:47:05Z")

</div>

```auto
const holidays = {p3};

```

I think this looks weird. Are you trying to establish an array here?

---

<div class="post-metadata">

**Author:** ![epdarias](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/epdarias/32/45740_2.png) [@epdarias](https://community.glideapps.com/u/epdarias)\
**Post date:** [July 13, 2023, 8:03am UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/3 "2023-07-13T08:03:57Z")

</div>

When I’ve used javascript in Glide, normally I need to convert variables (p1, p2, p3) in types that I need. For example: If you pass p3 as joined\_holidays like ‘1,2,3,4,5’, you need to do…

```auto
const holidays = p3.split(',').map(Number);

```

You also can force convert p2 to int with parseInt…

```auto
const numDays = parseInt(p2);

```

And in date case, you need to pass p1 with a date format like “yyyy-MM-dd” and try convert to string before convert to date…

```auto
const startDate = new Date(String(p1)); // 2023-07-13

```

Try this and comment.

---

<div class="post-metadata">

**Author:** ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)\
**Post date:** [July 13, 2023, 10:00am UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/4 "2023-07-13T10:00:30Z")

</div>

You also don’t have any code to return a value. It may be calculating futureDate, but it’s just sitting there. You need to add the following to actually return that value.

`return futureDate;`

---

<div class="post-metadata">

**Author:** ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)\
**Post date:** [July 13, 2023, 11:40am UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/5 "2023-07-13T11:40:41Z")

</div>

Hola @Thais_Fontes

Months ago we had a similar case and this was my solution. Although it can be done via JS, I preferred to use a no code one (I read it twice and I don’t even believe it myself 🤣)

> [@Excel formula Workdays](https://community.glideapps.com/t/excel-formula-workdays/43674/4):
>
> Hola @Alexey_Khrolovich There are 2 tricks to get this (one is easier than the other): [image] You must convert Glide date to a value using this format MM/dd/yyyy, you might use the Format Date plugin to achieve this quickly: Later, configure Excel formula plugin using a new date column. You will get a number and not a real date value due to Workday() formula in GS works in this way. Each value shown is the number of days since Dec 30, 1899 so, you have to convert…

Feliz día!

---

<div class="post-metadata">

**Author:** ![Thais\_Fontes](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thais_fontes/32/61251_2.png) [@Thais\_Fontes](https://community.glideapps.com/u/Thais_Fontes)\
**Post date:** [July 13, 2023, 9:19pm UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/6 "2023-07-13T21:19:39Z")

</div>

You are right!  
I had tried this before but I believe I was using the wrong date format for the workday() formula to work.

Thanks!!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [July 14, 2023, 9:20pm UTC](https://community.glideapps.com/t/javascript-to-calculate-work-days/63838/7 "2023-07-14T21:20:29Z")

</div>

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