Counting the Total Number of Days using Script from Business Days

To count the business days backward use a while loop to move one day back and check whether it is a weekend or not using day numbers 0 to 6. Each day of a week start from Sunday that is 0 and ends with Saturday that is 6, use getDay() to get the number of the day.

If the number of the day is either 0 or 6 the weekends count is increased by one. if it is not equal to 0 or 6 it will increment the iterating variable i.

After iterating the while loop for BUSINESS_DAYS it will add the weekend days count with BUSINESS_DAYS and total number of days is calculated.

        

        let weekEndsCount = 0;

        let i = 1

        // Loop until we reach BUSINESS_DAYS business days ago

        while (i <= BUSINESS_DAYS) {

          // Move the date 1 day back

          currentDate.setDate(currentDate.getDate() – 1);

          // Check if the current day is not Saturday or Sunday

          if (currentDate.getDay() === 0 || currentDate.getDay() === 6) {

            // Increment the business days count

           weekEndsCount++;

          } else {

            i++;}

        }

        let daysCount = weekEndsCount + BUSINESS_DAYS;

Here daysCount gives the number of days need to go back.

Leave a comment

Your email address will not be published. Required fields are marked *