calculate working days excluding weekends and holidays

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
rvuro
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Aug 26, 2020 6:25 pm

Wed Aug 26, 2020 6:36 pm

The script below only output days excluding weekends, i want to exclude holidays as well.

Code: Select all

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: Select all

<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>
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 396
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Thu Aug 27, 2020 6:59 am

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: Select all

    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.
Life is a fig newton of your imagination! https://www.miniaturephotographer.com
Post Reply