Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the total days between two selected calendar dates

Let say i have 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.

Anyone can please help on this?

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;

}
like image 457
Bryan Avatar asked Apr 29 '15 03:04

Bryan


People also ask

How to calculate the number of days between two dates?

The next method of calculating days between two dates uses the DAYS function. The DAYS function takes the end date and start date to return the number of days between the two dates. That suits us!

How to find total years and months in Excel?

1. Use DATEDIF to find the total years. In this example, the start date is in cell D17, and the end date is in E17. In the formula, the “y” returns the number of full years between the two days. 2. Use DATEDIF again with “ym” to find months.

What is the days function in Excel?

The DAYS function takes the end date and start date to return the number of days between the two dates. That suits us! Let’s apply the following DAYS function formula to our example:

How to calculate time of service in Excel?

You can also calculate age or someone’s time of service. The result can be something like “2 years, 4 months, 5 days.”. 1. Use DATEDIF to find the total years. In this example, the start date is in cell D17, and the end date is in E17. In the formula, the “y” returns the number of full years between the two days. 2.


2 Answers

The following function calculates the number of business days between two dates

function getBusinessDatesCount(startDate, endDate) {
    var count = 0;
    var curDate = startDate;
    while (curDate <= endDate) {
        var dayOfWeek = curDate.getDay();
        if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
           count++;
        curDate.setDate(curDate.getDate() + 1);
    }
    return count;
}


//Usage

var startDate = new Date('7/16/2015');
var endDate = new Date('7/20/2015');
var numOfDates = getBusinessDatesCount(startDate,endDate);
$('div#result').text(numOfDates);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"/>
like image 158
Vadim Gremyachev Avatar answered Oct 12 '22 18:10

Vadim Gremyachev


First you have to calculate the difference in time, then convert the time to days

var calculateDifference = function(date1, date2){
    var timeDifference = Math.abs(date2.getTime() - date1.getTime());
    return Math.ceil(timeDifference / (1000 * 3600 * 24));//ms * seconds * hours
}

var difference = calculateDifference(new Date("7/16/2015"), new Date("7/20/2015"));

untested, but should work...

like image 33
Kevin Kuyl Avatar answered Oct 12 '22 20:10

Kevin Kuyl