calculate between 2 date that exclude weekend using .change javascript

Viewed 54

I already get the difference between two date . Now I need to exclude the weekend and display it in the duration input. For Example : I Choose the date from ( 2 March 2020 ) to (9 March 2020) The duration should display 6 days , because it need to deduct 2 day which is saturday and sunday . 2/

$(document).ready(function(){
$('#FromDate').change(function(){

ToDate.min=document.getElementById('FromDate').value;
var start = new Date (document.getElementById('FromDate').value);
var end = new Date (document.getElementById('ToDate').value);
var duration = new Date();

var different = end.getTime() - start.getTime();
duration = (different/(1000*60*60*24))+1;
document.getElementById('duration').value = duration;
});


$('#ToDate').change(function(){
var start = new Date (document.getElementById('FromDate').value);
var end = new Date (document.getElementById('ToDate').value);
var duration = new Date();

var different = end.getTime() - start.getTime();
duration = (different/(1000*60*60*24))+1;
document.getElementById('duration').value = duration;
});

});
2 Answers

Here's a solution that iterates over the dates to find out how many weekends are included:

// this value is fetched again in order to keep the original value of 'start' from changing
let dateInRange = new Date (document.getElementById('FromDate').value);
let numberOfWeekendDaysInRange = 0;
// here we are making use of the existing 'end' object
while (dateInRange.toISOString() < end.toISOString()) {
        if (isWeekend(dateInRange)) {
            numberOfWeekendDaysInRange += 1;
    }
    // add a day; this internally takes care of shifting months and years
    dateInRange = new Date(dateInRange.getFullYear(), dateInRange.getMonth(), dateInRange.getDate() + 1);
}

function isWeekend(date) {
    // 0 is sunday, 6 is saturday
    return date.getDay() === 0 || date.getDay() === 6;
}

All that is left for you is to subtract the numberOfWeekendDaysInRange value from your formula.

Consult Date docs for methods used.

I Have Found the solution

function excludeweekend (startDate, endDate) {
    var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;
    var ifThen = function (a, b, c) {
        return a == b ? c : a;
    };

    elapsed = endDate - startDate;
    elapsed /= 86400000;

    daysBeforeFirstSunday = (7 - startDate.getDay()) % 7;
    daysAfterLastSunday = endDate.getDay();

    elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);
    elapsed = (elapsed / 7) * 5;
    elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);

    return Math.ceil(elapsed);
}


//duration calculation
$(document).ready(function(){
$('#FromDate').change(function(){

ToDate.min=document.getElementById('FromDate').value;
var start = new Date (document.getElementById('FromDate').value);
var end = new Date (document.getElementById('ToDate').value);
var duration = new Date();

var different = end.getTime() - start.getTime();
// duration = (different/(1000*60*60*24))+1;
duration=excludeweekend(new Date(start), new Date(end));
document.getElementById('duration').value = duration;
});


$('#ToDate').change(function(){
var start = new Date (document.getElementById('FromDate').value);
var end = new Date (document.getElementById('ToDate').value);
var duration = new Date();

var different = end.getTime() - start.getTime();
// duration = (different/(1000*60*60*24))+1;
duration=excludeweekend(new Date(start), new Date(end));
document.getElementById('duration').value = duration;
});
});
Related