Home > Mobile >  Calculate days off in react native
Calculate days off in react native

Time:03-15

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
  }

CodePudding user response:

currentDate = currentDate.addDays(1)

.addDays is not a correct function of javascript. It is 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))
      }

CodePudding user response:

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"));

  • Related