How to calculate the total days between two selected calendar dates
Asked Answered
P

4

11

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;

}
Ply answered 29/4, 2015 at 3:4 Comment(0)
I
18

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"/>
Illustration answered 1/5, 2015 at 18:3 Comment(2)
According to w3schools.com/jsref/jsref_obj_date.asp getDate() returns the day of the month. Is that what you intended?Selene
This fails when time change from summer to winter time happen. E.g. startDate 10/27/17 and endDate 10/30/17. This will count only 1 day instead of 2 days.Led
D
1

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...

Doralyn answered 29/4, 2015 at 3:25 Comment(0)
N
0

Using moment startDate and endDate:

function getBusinessDaysCount(startDate, endDate) {
  let relativeDaySequence = [...Array(endDate.diff(startDate, "days")).keys()];
  let daySequence = relativeDaySequence.map(relativeDay => {
    let startDateClone = startDate.clone();
    return startDateClone.add(relativeDay, "days");
  });
  return daySequence.reduce(
    (dayCount, currentDay) => dayCount + (currentDay.day() === 0 || currentDay.day() === 6 ? 0 : 1),
    0
  );
}
Natty answered 1/9, 2018 at 20:27 Comment(0)
Z
-1

Can Try this it will also work.

//fromDate is start date and toDate is end Date
    var timeDiff = Math.abs(toDate.getTime() - fromDate.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)) ;
//  var startDay = fromDate.getDay();
    debugger;
    var diffDayWeek = diffDays + fromDate.getDay();
    var diffDiv = Math.floor(parseInt(diffDayWeek/7));
    diffDiv *= 2; "Saturday and Sunday are Weekend
    diffDays += 1 - diffDiv;
Zigzagger answered 19/2, 2016 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.