How to calculate the total days between two selected calendar dates - javascript

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;
}

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"/>

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

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
);
}

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;

Related

Return list of date between specific range selected from datepicker (include and exclude weekends)

I try to code with JavaScript and now, I have another problem. The intended goal is to calculate the number of days and to return list of those date between two selected dates from datepicker. The trick is: the system should provide two option: during all day of the week (including Saturday and Sunday) and only during working days. I tried to do this on my own, but I am stuck. I have also tried to find out if anyone happened to asked the same question, but the result is no. Here is my code. Can anyone help me with this? Thank you!
$("#calculate").click(function() {
// TO CALCULATE TOTAL DAYS
startdate.setHours(0, 0, 0, 1);
enddate.setHours(23, 59, 59, 999);
var days = Math.ceil(enddate - startdate) / (1000 * 60 * 60 * 24);
var weeks = Math.floor(days / 7);
days = days - (weeks * 2);
var month = startdate.getMonth();
var day = startdate.getDate();
var year = startdate.getFullYear();
// ONLY DURING WORKING DAYS
$("#weekday").change(function() {
if ($(this).is(':checked')) {
var startday = startdate.getDay();
var endday = enddate.getDay();
if (startday - endday > 1) {
days = days - 2
}
if (startday == 0 && endday != 6) {
days = days - 1
}
if (endday == 6 && startday != 0) {
days = days - 1
}
// TO SHOW THE LIST OF DATE BETWEEN START AND END DATE
while (startdate <= enddate) {
if (startday < 6 && startday > 0) {
month = startdate.getMonth() + 1
}
if (month <= 9) {
month = "0" + month
}
var day = startdate.getdate();
if (day <= 9) {
day = "0" + day
}
}
startdate.setDate(startdate.getDate() + 1);
}
})
})
var quantity = $("#quantity").val();
var delivery = Math.round(quantity / days);
var datearray = document.write(day + "." + month + "." + startdate.getFullYear());
if (enddate < startdate) {
alert("Start date must be earlier than end date!")
} else if (quantity == 0) {
alert("Quantity must be bigger than 0")
} else {
append("<br>Total " + days + " selected days<br/>");
append("<br>Shipment" + delivery + " pc(s) per day<br/>");
append("<br>During period of" + datearray + "<br/>")
}
});

Time Zone Countdown script, not working as intended?

I was on the lookout for a javascript countdown with adjustable timezones, and found this script called tzcount.js. According to the instructions:
The month can be specified as a number between 1 and 12 to indicate
which month of the year that you are counting down to (it will assume
next year if the month has already past for this year)
But when I enter the value 1 for month, the script tells me that the date has passed, instead of assuming it is the first month of the next year. Am I missing something or is this script not working as intended?
The full script:
<!--Copy and paste just above the close </BODY> of you HTML webpage.-->
<SCRIPT type="text/javascript">
// **** Time Zone Count Down Javascript **** //
/*
Visit http://rainbow.arch.scriptmania.com/scripts/
for this script and many more
*/
////////// CONFIGURE THE COUNTDOWN SCRIPT HERE //////////////////
var month = '*'; // '*' for next month, '0' for this month or 1 through 12 for the month
var day = '1'; // Offset for day of month day or + day
var hour = 0; // 0 through 23 for the hours of the day
var tz = -5; // Offset for your timezone in hours from UTC
var lab = 'tzcd'; // The id of the page entry where the timezone countdown is to show
function start() {displayTZCountDown(setTZCountDown(month,day,hour,tz),lab);}
// ** The start function can be changed if required **
window.onload = start;
////////// DO NOT EDIT PAST THIS LINE //////////////////
function setTZCountDown(month,day,hour,tz)
{
var toDate = new Date();
if (month == '*')toDate.setMonth(toDate.getMonth() + 1);
else if (month > 0)
{
if (month <= toDate.getMonth())toDate.setYear(toDate.getYear() + 1);
toDate.setMonth(month-1);
}
if (day.substr(0,1) == '+')
{var day1 = parseInt(day.substr(1));
toDate.setDate(toDate.getDate()+day1);
}
else{toDate.setDate(day);
}
toDate.setHours(hour);
toDate.setMinutes(0-(tz*60));
toDate.setSeconds(0);
var fromDate = new Date();
fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
var diffDate = new Date(0);
diffDate.setMilliseconds(toDate - fromDate);
return Math.floor(diffDate.valueOf()/1000);
}
function displayTZCountDown(countdown,tzcd)
{
if (countdown < 0) document.getElementById(tzcd).innerHTML = "Sorry, you are too late.";
else {var secs = countdown % 60;
if (secs < 10) secs = '0'+secs;
var countdown1 = (countdown - secs) / 60;
var mins = countdown1 % 60;
if (mins < 10) mins = '0'+mins;
countdown1 = (countdown1 - mins) / 60;
var hours = countdown1 % 24;
var days = (countdown1 - hours) / 24;
document.getElementById(tzcd).innerHTML = days + " day" + (days == 1 ? '' : 's') + ' + ' +hours+ 'h : ' +mins+ 'm : '+secs+'s';
setTimeout('displayTZCountDown('+(countdown-1)+',\''+tzcd+'\');',999);
}
}
</SCRIPT>
<p><font face="arial" size="-2">The countdown script at </font><br><font face="arial, helvetica" size="-2">Rainbow Arch</font></p>
I found the script, and the instructions, here http://rainbow.arch.scriptmania.com/scripts/timezone_countdown.html
The script isn't working right.
You need to edit this line:
if (month <= toDate.getMonth())toDate.setYear(toDate.getYear() + 1);
getYear abbreviates, so it returns 115 for this year. setYear then thinks you mean 115 AD, which passed a long time ago.
Replace setYear and getYear with setFullYear and getFullYear. Those functions will return/expect 2015.

count 45 Working days after a selected date in javascript

I have one date parameter. 31 October 2014. i want to get next days = 45 days after 31 October 2014. So it should be 2 Januari 2015. and excluding Saturday and Sunday.
I already make the function like this. But when i add 45. the 45 not excluding Saturday and Sunday.
function getNextBusinessDay() {
return getDeliveryDateObj(1);
}
function getDeliveryDateObj(businessDaysLeftForDelivery) {
var now = new Date();
var dayOfTheWeek = now.getDay();
now.setDate(now.getDate() + 45);
var calendarDays = businessDaysLeftForDelivery;
var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery;
if (deliveryDay >= 6) {
businessDaysLeftForDelivery -= 6 - dayOfTheWeek; //deduct this-week days
calendarDays += 2; //count this coming weekend
deliveryWeeks = Math.floor(businessDaysLeftForDelivery / 5); //how many whole weeks?
calendarDays += (deliveryWeeks * 2); //two days per weekend per week
}
now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000);
return now;
}
This addWeekdays function should do what you want.
function addWeekdays(date, weekdays) {
var newDate = new Date(date.getTime());
var i = 0;
while (i < weekdays) {
newDate.setDate(newDate.getDate() + 1);
var day = newDate.getDay();
if (day > 1 && day < 7) {
i++;
}
}
return newDate;
}
var currentDate = new Date('10/31/2014');
var targetDate = addWeekdays(currentDate, 45);
alert(targetDate);
Here's how I solved my problem:
function test() {
var startDate = "31-oct-2014";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 45, count = 0;
while (count < noOfDaysToAdd) {
endDate = new Date(startDate.setDate(startDate.getDate() + 1));
if (endDate.getDay() != 0 && endDate.getDay() != 6) {
count++;
}
}
$("#Test").val(endDate);
}

Datepicker set by day of year

This is a followup to a previous question about Datepicker. Trying to show only every third day on the calendar. figured out how to get DOY as integer and use in calendar, however it fails every March - also not able to span two years?
On a second part, if I need to disable a certain day of the week, how do I combine that with current 3rd day feature.
here is JSFiddle DatePicker
function noWeekEnds(date) {
var dow = date.getDay();
if(dow>5 || dow<1) return [false,''];
return [true,''];
}
function unavailable(date) {
var now = date;
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var shift = (day%3===0);
return [shift, "red2", "available"];
return noWeekEnds(date);
/*
need this to span across 2 years i.e.: Jan 8 2015 thru Jan 12/2016
also it fails the 3rd week of every March ???
*/
}
$(document).ready(function() {
$("#datepicker").datepicker({
beforeShowDay: unavailable
});
$('#datepicker').attr('readonly',true);
});
Fixed unavailable function:
http://jsfiddle.net/nbL98a2r/13/
function unavailable(date) {
var start = new Date(2015,0,8);
var end = new Date(2016,0,12);
var now = date;
if(now < start || now > end) return [false, "red2", "available"]
var timeDiff = Math.abs(now.getTime() - start.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
var nwe = noWeekEnds(date);
var shift = (diffDays%3===0) && nwe[0];
return [shift, "red2", "available"]
}

Find day difference between two dates (excluding weekend days)

Hi i am using jquery-ui datepicker to select date and date.js to find difference between 2 dates.
Right now the problem is I want to exclude weekend days from calculation (saturday and sunday). How should i do that?
For example the user select start date (13/8/2010) and end date (16/8/2010). Since 14/8/2010 and 15/8/2010 is in week days, instead of 4 days total, i want it to be only 2 days.
This is the code im using right now:
<script type="text/javascript">
$("#startdate, #enddate").change(function() {
var d1 = $("#startdate").val();
var d2 = $("#enddate").val();
var minutes = 1000*60;
var hours = minutes*60;
var day = hours*24;
var startdate1 = getDateFromFormat(d1, "d-m-y");
var enddate1 = getDateFromFormat(d2, "d-m-y");
var days = 1 + Math.round((enddate1 - startdate1)/day);
if(days>0)
{ $("#noofdays").val(days);}
else
{ $("#noofdays").val(0);}
});
</script>
Maybe someone else can help you converting this function into JQuery's framework...
I found this function here.
function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1) return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
if (iWeekday1 < iWeekday2) { //Equal to makes it reduce 5 days
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
return (iDateDiff + 1); // add 1 because dates are inclusive
}
var date1 = new Date("August 11, 2010 11:13:00");
var date2 = new Date("August 16, 2010 11:13:00");
alert(calcBusinessDays(date1, date2));
## EDITED ##
If you want to use it with your that format just:
Your code will look like:
function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1) return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
if (iWeekday1 < iWeekday2) { //Equal to makes it reduce 5 days
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
return (iDateDiff + 1); // add 1 because dates are inclusive
}
$("#startdate, #enddate").change(function() {
var d1 = $("#startdate").val();
var d2 = $("#enddate").val();
var minutes = 1000 * 60;
var hours = minutes * 60;
var day = hours * 24;
var startdate1 = new Date(d1);
var enddate1 = new Date(d2);
var newstartdate = new Date();
newstartdate.setFullYear(startdate1.getYear(), startdate1.getMonth(), startdate1.getDay());
var newenddate = new Date();
newenddate.setFullYear(enddate1.getYear(), enddate1.getMonth(), enddate1.getDay());
var days = calcBusinessDays(newstartdate, newenddate);
if (days > 0) {
$("#noofdays").val(days);
} else {
$("#noofdays").val(0);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Start Date
<input type="date" id="startdate" value="2019-03-03"/>
</label>
<label>End Date
<input type="date" id="enddate" value="2019-03-06"/>
</label>
<label>N. of days
<output id="noofdays"/>
</label>
To do this, you should NOT search all days between these dates !
It's not complicated, look some evident assumptions:
All full-week has 7-days.
Which 2 are weekend-days.
And which 5 are business-day.
Evident conclusions:
Look all days is loss of time.
Check what day is weekend to all week is loss of time.
Without tedious explanation.. let me show the code:
function getBusinessDateCount (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);
}
function calc() {
let start = document.querySelector('#startDate').value,
end = document.querySelector('#endDate').value,
result = getBusinessDateCount(new Date(start), new Date(end));
document.querySelector('#result').value = result;
}
Start date: <input type="date" id="startDate" value="2020-01-04"><br>
End date: <input type="date" id="endDate" value="2020-01-06"><br>
<input type="button" onclick="calc()" value="Get business days"><br>
Business days: <input id="result" readonly>
You can test it yourself with any dates.
I just want to notice that this code ONLY consumed 0.43 sec between dates from 2000 to 2015... It is much more fast than some other codes.
Hope it helps...
Nice coding !!
This is how I would do it
function getDays(d1, d2) {
var one_day=1000*60*60*24;
var d1_days = parseInt(d1.getTime()/one_day) - 1;
var d2_days = parseInt(d2.getTime()/one_day);
var days = (d2_days - d1_days);
var weeks = (d2_days - d1_days) / 7;
var day1 = d1.getDay();
var day2 = d2.getDay();
if (day1 == 0) {
days--;
} else if (day1 == 6) {
days-=2;
}
if (day2 == 0) {
days-=2;
} else if (day2 == 6) {
days--;
}
days -= parseInt(weeks) * 2;
alert(days);
}
getDays(new Date("June 8, 2004"),new Date("February 6, 2010"));
EDIT
To clarify my comment to #keenebec...
That solution will work for small date differences quite nicely and is easy to understand. But take something as "short" as a 6 year span and you can see a remarkable difference in speed.
http://jsfiddle.net/aSvxv/
I included all 3 answers and the original answer is indeed the fastest, but not by much and the trade off for a few microseconds of execution is somewhat trivial to me in favor of readability.
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf())
date.setDate(date.getDate() + days);
return date;
}
function getBusinessDatesCount(startDate, endDate) {
var count = 0;
var curDate = startDate;
while (curDate <= endDate) {
var dayOfWeek = curDate.getDay();
var isWeekend = (dayOfWeek == 6) || (dayOfWeek == 0);
if(!isWeekend)
count++;
curDate = curDate.addDays(1);
}
return count;
}
//Usage
var startDate = new Date('7/16/2015');
var endDate = new Date('7/20/2015');
var numOfDays = getBusinessDatesCount(startDate,endDate);
jQuery('div#result').text(numOfDays);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"/>
That looks like too much work to me. I'd rather let the computer do the heavy lifting-
//
Date.bizdays= function(d1, d2){
var bd= 0, dd, incr=d1.getDate();
while(d1<d2){
d1.setDate(++incr);
dd= d1.getDay();
if(dd%6)++bd;
}
return bd;
}
//test
var day1= new Date(2010, 7, 11), day2= new Date(2010, 7, 31);
alert(Date.bizdays(day1, day2))
To understand way.,
Actual days = 14
weeks for Actual days = 14/7=2
Weekends per week=2
Total weekends=2*weeks for days
So apply this ,
$('#EndDate').on('change', function () {
var start = $('#StartDate').datepicker('getDate');
var end = $('#EndDate').datepicker('getDate');
if (start < end) {
var days = (end - start) / 1000 / 60 / 60 / 24;
var Weeks=Math.round(days)/7;
var totalWeekends=Math.round(Weeks)*2;
var puredays=Math.round(days)-totalWeekends;
$('#days').text(Math.round(puredays) + "Working Days");
}
else {
alert("");
}
Thank you !
Important: Most answers here don't actually work if the start date (or sometimes the end date) is a saturday or sunday. I took the accepted response and modified it so that this issue is resolved now:
var dateDiff;
if (dateTo < dateFrom) return -1; // error code if dates transposed
var dateFromDayOrig = dateFrom.getDay(); // day of week
var dateToDayOrig = dateTo.getDay();
var dateFromDay = (dateFromDayOrig == 0) ? 7 : dateFromDayOrig; // change Sunday from 0 to 7
var dateToDay = (dateToDayOrig == 0) ? 7 : dateToDayOrig;
dateFromDay = (dateFromDay > 5) ? 5 : dateFromDay; // only count weekdays
dateToDay = (dateToDay > 5) ? 5 : dateToDay;
// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
var weekDifference = Math.floor((dateTo.getTime() - dateFrom.getTime()) / 604800000);
if (dateFromDay <= dateToDay) {
dateDiff = (weekDifference * 5) + (dateToDay - dateFromDay);
} else {
dateDiff = ((weekDifference + 1) * 5) - (dateFromDay - dateToDay);
}
// fix: remove one day if it's saturday or sunday
if (dateFromDayOrig >= 6 || dateFromDayOrig == 0) {
dateDiff--;
}
return (dateDiff + 1); // add 1 because dates are inclusive
There seems to be few issues with the response that has been marked as solution.
The statement setFullYear() is returning incorrect value if I choose start date as 06/11/2015. So instead, the startDate1 and endDate1 can be directly passed to the function.
If the start date is Saturday or Sunday, still the code is counting it(iWeekday1) as 5 days
If the end date is Saturday or Sunday, still the code is counting it(iWeekday2) as 5 days. But these 5 days already get counted in the iweeks calculation.
So instead of
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
it should be
iWeekday1 = (iWeekday1 > 5) ? 0 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 0 : iWeekday2;
The last IF condition should be executed when start and end date day is same like both are on same day, the date could be different
if (iWeekday1 <= iWeekday2)
The condition that adjusts if both days are weekends can be removed
iDateDiff -= iAdjust
Lastly, the +1 should be done only if start and end date falls on weekdays. Currently, it is adding in both the cases.
return (iDateDiff + 1);//Add condition to apply only if both days are weekdays
--can't comment on that answer as I do not have that reputation :)
I get it work with this code. Note that the function is from date.js and businessday js (thanks to Garis Suero). Start Date 11-08-2010 End Date 16-08-2010 will result 4 days of leave.
<script type="text/javascript">
$("#startdate, #enddate").change(function() {
var d1 = $("#startdate").val();
var d2 = $("#enddate").val();
var minutes = 1000*60;
var hours = minutes*60;
var day = hours*24;
var startdate1 = getDateFromFormat(d1, "d-m-y");
var enddate1 = getDateFromFormat(d2, "d-m-y");
var days = calcBusinessDays(new Date(startdate1),new Date(enddate1));
if(days>0)
{ $("#noofdays").val(days);}
else
{ $("#noofdays").val(0);}
});
</script>
What I did
function calcbusinessdays()
{
for(var c=0,e=0,d=new Date($("#startdate").val()),a=(new Date($("#enddate").val())-d)/864E5;0<=a;a--)
{
var b=new Date(d);
b.setDate(b.getDate()+a);
1==Math.ceil(b.getDay()%6/6)?c++:e++
}
$("#noofdays").html(c)
};
c is weekdays, e is weekends
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
var currentDate;
selectFlixbleDates = [];
var monToSatDateFilter=[];
currentDate=new Date(date);
while(currentDate){
console.log("currentDate"+currentDate);
if(new Date(currentDate).getDay()!=0){
selectFlixbleDates.push(currentDate)
}
if(selectFlixbleDates.length==$scope.numberOfDatePick)
{
break;
}
currentDate=addDays(currentDate,1);
}
for (var i = 0; i < selectFlixbleDates.length; i++) {
// console.log(between[i]);
monToSatDateFilter.push((selectFlixbleDates[i].getMonth() + 1) + '/' + selectFlixbleDates[i].getDate() + '/' + selectFlixbleDates[i].getFullYear());
}
var endDate=monToSatDateFilter.slice(-1).pop();
var space =monToSatDateFilter.join(', ');
var sdfs= document.getElementById("maxPicks").value =space;
$scope.$apply(function() {
$scope.orderEndDate=monToSatDateFilter.slice(-1).pop()
$scope.orderStartDate=monToSatDateFilter[0];
});
document.getElementById("startDateEndDate").innerHTML =$scope.orderStartDate+ ' TO ' +$scope.orderEndDate
}
I have used Angular framework and Moment.js library to implement the solution.
My solution covers all the cases.
this.daysInBetween = this.endMoment.diff(this.startMoment, 'days') + 1;
this.weeksInBetween = this.endMoment.diff(this.startMoment, 'weeks');
this.weekDays = this.daysInBetween - (this.weeksInBetween * 2);
if ( (this.startMoment.day() === 0 && this.endMoment.day() === 6) ||
(this.startMoment.day() > this.endMoment.day()) ) {
// IF ONE WEEKEND WAS MISSED
this.weekDays-=2;
} else if ( this.startMoment.day() <= this.endMoment.day() &&
( this.startMoment.day() === 0 || this.startMoment.day() === 6 ||
this.endMoment.day() === 0 || this.endMoment.day() === 6) ) {
// IF EITHER OF DAYS WAS A WEEKEND
this.weekDays--;
}
Live Demo:
Calculate number of weekdays
I am currently working on a blog to write about my approach to this
specific problem. I will post the link to the blog on the comment.
Important: Most answers here don't actually work if the start date (or sometimes the end date) is a saturday or sunday.
For example: if your start and end date are
Saturday to Sunday or vice versa
or Saturday to Saturday
or Sunday to Sunday
So, here is the modified answer from the accepted answer
function calculateBusinessDays(dDate1, dDate2) {
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1) {
return -1;
}
var iWeekday1 = dDate1.getDay();
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 === 0) ? 7 : iWeekday1;
iWeekday2 = (iWeekday2 === 0) ? 7 : iWeekday2;
if (iWeekday1 > 5 && iWeekday2 <= 6) {
iWeekday1 = 0;
iAdjust = 1;
}
iWeekday2 = (iWeekday1 === 0 && iWeekday2 === 6) ? 0 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1;
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1;
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000);
if (iWeeks===0 && iWeekday1===0 && iWeekday2===0
&& (dDate2.getTime() !== dDate1.getTime()) ) {
iWeeks = 1;
}
if (iWeekday1 <= iWeekday2) {
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust
return (iDateDiff + 1);
}

Categories

Resources