The problem with those solutions is that they loop through all the days between the dates so if the dates are thousands of years apart it will get very slow.
The earlier solution given gets the answer without looping.
Huge thanks for the great replies guys, some good stuff here.
Think I’ll go with mrhoo’s then; going to give it a go now!
One final question (sorry!) the date picker I’m using has the fields layed out with names like…
date-dd-1
… but this won’t work in Javascript as it doesn’t like picking elements with the dash in them. Anyway around this please? If I rename them, the picker stops working
I’ve changed it so that it adds one to the value (if the start date is the same day as the last day they were absent, they’ve been off for a day…) it returns like so…
function datedifference(d1, d2){
var diff= Math.abs(d1-d2);
var days= Math.floor(diff/8.64e7);
var tem= days%7;
var weeks= Math.floor(days/7);
var wd1= d1.getDay();
var wd2= d2.getDay();
if(wd1== 6) tem-= 2;
else if(wd1== 0) tem-= 1;
if(wd2== 0) tem-= 1;
return weeks*5+tem+1;
}
If I select the start date as ‘Thu Jan 15 00:00:00 UTC 2009’, then select the same end date (Thu Jan 15 00:00:00 UTC 2009) it returns1 days, which is fine.
If I select the next day (Fri Jan 16 00:00:00 UTC 2009), it returns 2, which again is fine, but then it goes a little wrong…
Sat Jan 17 00:00:00 UTC 2009 - returns 3, should return 1 as it’s a weekend
Sun Jan 18 00:00:00 UTC 2009 - returns 3, it’s detecting sunday which is good
Mon Jan 19 00:00:00 UTC 2009 - jumps to 5, not sure why
Tue Jan 20 00:00:00 UTC 2009 - returns 6, should be just four I think
Wed Jan 21 00:00:00 UTC 2009 - returns 7
Thu Jan 22 00:00:00 UTC 2009 - returns 6 (???)
Fri Jan 23 00:00:00 UTC 2009 - returns 7
Sat Jan 24 00:00:00 UTC 2009 - returns 8, but shouldn’t as it’s a weekend
Sun Jan 25 00:00:00 UTC 2009 - returns 8, discounts the Sunday again, which is good
Mon Jan 26 00:00:00 UTC 2009 - returns 10, jumps again on a Monday
… and repeat, huge apologies for the long post, but I’m really a bit stuck, any help would be fantastic, thanks!
If you count from a tuesday and stop on a monday, or thursday to wednesday, for example, there is an odd weekend that needs to be accounted for.
It is simpler to first set any weekend start date to the first business day following the start date, and any weekend end date to the last business day before the end date.
This is the larger part of the code, and is not needed if you do not allow weekends in your date picker
function bizdays(d1, d2){
var wd1= d1.getDay();
var wd2= d2.getDay();
// adjust start and end dates
var n1= wd1== 0? 1: wd1== 6? 2: 0;
var n2= wd2== 0? -2: wd2== 6? -1: 0;
if(n1){
d1.setDate(d1.getDate()+n1);
wd1= 1;
}
if(n2){
d2.setDate(d2.getDate()+n2);
wd2= 5;
}
// calculate week days
var days= Math.floor((d2-d1)/8.64e7);
if(days< 0) days= 0;
var tem= days%7;
var weeks= Math.floor(days/7);
// catch the odd weekend
if(wd1> wd2) tem-= 2;
// if you want to subtract holidays, do it now-
// don't count week end holidays twice!
// return business days
return weeks*5+tem;
}
// quick test
function testbizdays(n2){
var D1= new Date(2009, 0, 1);
var D2= new Date(2009, 0, n2);
return D2.toLocaleDateString()+' +'+bizdays(D1, D2)+' biz days';
}
var DA= [], D= new Date(2009, 0, 1),i= 1;
DA[0]= 'Start date: '+D.toLocaleDateString();
while(i< 30){
DA[DA.length]= testbizdays(i++);
}
alert(DA.join('\
'));