1

I'm trying to create a method to calculate the total days off.

I have an array that contains the working days:

"workingDays": [1,2,3,4,5],

So I have two dates; a startDate and an endDate, and I should count in this range how many days aren't working days (i.e. total days off).

For example, I have a range from 03/15 (today) to 03/21, a total of 7 days.

Today (03/15) is day 2, and it is a working day so I don't have to increment a counter for days off, while for example 03/19 is not a working day (it is number 6) so I have to increment the day off variable.

I have tried to implement the code, but it doesn't work correctly:

const checkDate = (start, end) => {
    const workingDays = [1,2,3,4,5]
    let dayOff = 0
    var currentDate = new Date(start)
    while (currentDate <= end) {
      workingDays.map((day) => {
        console.log('current ', currentDate.getDay(), ' day ', day)
        if (currentDate.getDay() !== day) {
          dayOff += 1
        }
        currentDate = currentDate.addDays(1)
      })
    }
    console.log('dayOff ', dayOff)
    return dayOff

  }

it prints me:

LOG  current  2  day  1
 LOG  current  3  day  2
 LOG  current  4  day  3
 LOG  current  5  day  4
 LOG  current  6  day  5
 LOG  current  0  day  1
 LOG  current  1  day  2
 LOG  current  2  day  3
 LOG  current  3  day  4
 LOG  current  4  day  5
 LOG  dayOff  10

but it is wrong because the result should be 2.

How can I do? Thank you

EDIT.

The function that I use to add a day:

 Date.prototype.addDays = function (days) {
    var date = new Date(this.valueOf())
    date.setDate(date.getDate() + days)
    return date
  }
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42
Jack23
  • 1,368
  • 7
  • 32

2 Answers2

2
currentDate = currentDate.addDays(1)

.addDays is not a correct function of Javascript. It's used in C#.

Below is the right code which will return correct answer:

    const workingDays = [1,2,3,4,5]
const endDate = new Date(startDate)
    let dayOff = []
    var currentDate = new Date(endDate)
    
    while (currentDate <= endDate) {
        console.log('currentDate', currentDate)
        let index = workingDays.indexOf(currentDate.getDay());

        console.log('dayOff - index', index)
        if(index == -1)
        {
            console.log('dayOffDate - addded', currentDate)
            dayOff.push(currentDate)
        }
        currentDate = new Date(currentDate.setDate(currentDate.getDate() + 1))
      }
Dani
  • 1,825
  • 2
  • 15
  • 29
  • you can even refer https://stackoverflow.com/questions/563406/how-to-add-days-to-date –  Mar 15 '22 at 10:00
  • I forgot to insert the function that I use to add a day – Jack23 Mar 15 '22 at 10:02
  • ok let me check it again –  Mar 15 '22 at 10:09
  • Ok thank you, I have to check every date in the range with every element in array, while I'm checking date with the first element, if they are not equal I check the day + 1 with the second element of array ( while I should compare with the second array's element) – Jack23 Mar 15 '22 at 10:11
  • yes exactly. this will worked. –  Mar 15 '22 at 10:16
  • I have updated my original answer –  Mar 15 '22 at 10:44
  • @AjayPatel it is better in general to use the UTC versions of the `Date()` methods to avoid errors due to time/zone differences. – Mohsen Alyafei Mar 15 '22 at 11:15
  • @MohsenAlyafei I think I don't think so it is needed for the client side, But you work with different time zone then yes –  Mar 15 '22 at 11:21
1

The following small function will calculate the total days off between two (2) dates, given the first date and the end date.

It is simple and assumes the end date is the same or higher than the start date (does not check for reversing the dates).

function getTotalOffDays(start,end) {
    const workingDays = [1,2,3,4,5];
    let daysOff = 0,                  // days off counter
        date    = new Date(start),    // starting date
        endDate = new Date(end);      // end date
  while (date <= endDate) {
    if (!workingDays.includes(date.getUTCDay())) daysOff += 1;   // increment days off
    date = new Date(date.setUTCDate(date.getUTCDate() + 1));  // go the next day
  }
return daysOff;
}
console.log("Total days off: " + getTotalOffDays("2022-03-15", "2022-03-21"));
Mohsen Alyafei
  • 4,765
  • 3
  • 30
  • 42