# How to calculate the total days between two selected calendar dates

I've startDate = 7/16/2015 and endDate = 7/20/2015. This 2 dates are stored in a SharePoint list.

If user select the exact date with the date in SharePoint list, it can calculate the total days = 2 , which means that without calculate on the other days.

I use the following code to calculate the total day of difference without counting on weekend. But I cant figure out the way how to calculate the total day of selected date without counting on other days.

``````function workingDaysBetweenDates(startDate,endDate) {

// 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 = days - 1;

return days;

}
``````

Please guide me with proper solution

``````Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf())
date.setDate(date.getDate() + days);
return date;
}

var dates = new Array();
var curDate = startDate;
while (curDate <= endDate) {
var dayOfWeek = curDate.getDay();
var isWeekend = (dayOfWeek == 6) || (dayOfWeek == 0);
if(!isWeekend)
dates.push( new Date (curDate) )
}
return dates;
}

var startDate = new Date('7/16/2015');
var endDate = new Date('7/20/2015');

//usage
var numOfDays = dates.length;
console.log(numOfDays);
``````

JSFiddle