calculate working days excluding weekends and holidays

A

Anonymous

Guest
The script below only output days excluding weekends, i want to exclude holidays as well.
Code:
function workingDaysBetweenDates(startDate,endDate,holidays) {
var starDate = document.getElementById("s");
var endDate = document.getElementById("e");
startDate = new Date(s.value);
endDate = new Date(e.value);
// Validate input
if (endDate < startDate)
    return 'Invalid !';

// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
startDate.setHours(0,0,0,1);  // Start just after midnight
endDate.setHours(23,59,59,999);  // End just before midnight
var diff = endDate - startDate;  // Milliseconds between datetime objects    
var days = Math.ceil(diff / millisecondsPerDay);

// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
var days = days - (weeks * 2);

// Handle special cases
var startDay = startDate.getDay();
var endDay = endDate.getDay();

// Remove weekend not previously removed.   
if (startDay - endDay > 1)         
    days = days - 2;


// Remove start day if span starts on Sunday but ends before Saturday
if (startDay == 0 && endDay != 6)
    days = days - 1; 

// Remove end day if span ends on Saturday but starts after Sunday
if (endDay == 6 && startDay != 0)
    days =  - 1;

// Remove holidays


var result = document.getElementById("result");
result.value=days;


}

Code:
<p>Start:<input type="date" id="s" onchange="workingDaysBetweenDates()" /></p>
<p>End:  <input type="date" id="e" onchange="workingDaysBetweenDates()" /></p>
<p>Days: <input type="text" id="result"  /></p>
 
You are just going to have to figure out the holidays OOPS, I thought I was in the PHP Section, but the principle is the same.

Code:
    public function holidays() {

            $this->holidays[$this->year . "-01-01"] = "New Year's Day";
            if ($this->year > 1969 && $this->year < 2038) {
                $this->easter = new DateTime('@' . easter_date($this->year), new \DateTimeZone("America/Detroit"));
                $this->holidays[$this->easter->format("Y-m-d")] = "Easter Sunday";
            }
            $this->holidays[\date("Y-m-d", \strtotime("Last Monday of May " . $this->year))] = "Memorial Day";
            $this->holidays[$this->year . "-07-04"] = "4th of July";
            $this->holidays[\date("Y-m-d", \strtotime("First Monday of September " . $this->year))] = "Labor Day";
            $this->holidays[\date("Y-m-d", \strtotime("Fourth Thursday of November" . $this->year))] = "Thanksgiving Day";
            $this->holidays[$this->year . '-12-25'] = "Christmas Day";

        return $this->holidays;
    }

and then do what you have been doing. I just showed an example on how I figure them out and I didn't do all the holidays though figuring them out isn't that hard.
 
You have to specify first the days of the holidays in the array.



-----------------------------------------------------------
add your channel to the directory of telegram channels
 
Back
Top