javascript - How to calculate the total days between two selected calendar dates -
let have startdate = 7/16/2015 , enddate = 7/20/2015. 2 dates stored in sharepoint list.
if user select exact date date in sharepoint list, can calculate total days = 2 , means without calculate on other days.
anyone can please on this?
i use following code calculate total day of difference without counting on weekend. cant figure out way how calculate 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 after midnight enddate.sethours(23,59,59,999); // end before midnight var diff = enddate - startdate; // milliseconds between datetime objects var days = math.ceil(diff / millisecondsperday); // subtract 2 weekend days 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 removed. if (startday - endday > 1) days = days - 2; // remove start day if span starts on sunday ends before saturday if (startday == 0 && endday != 6) days = days - 1; // remove end day if span ends on saturday starts after sunday if (endday == 6 && startday != 0) days = days - 1; return days; }
the following function calculates number of business days between 2 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"/>
Comments
Post a Comment