How to get business days from the given two dates?

Hi All,

I am stuck with one function to get business days counts from the date which i will select like start date and end date. The range would be one year from start date to end date. In the between months i want the only business days count for one calculation. Can anyone help me on this?

You will need a list of what are not business days.

  • Weekends are not business days.
  • Public holidays aren’t either.

But, public holidays are different in different parts of the world. You will need to deal with that sort of issue before making much progress.

Something like this:

function busDayDiff(d1,d2,hols) {
function dayDiff(d1,d2) {
    return Math.floor(Math.abs(d1-d2)/86400000);
}
function between(d1, d2, d3) {
return (d3 > Math.min(d1,d2) && d3 < Math.max(d1,d2));
}
var d = dayDiff(d1,d2); 
var t= d%7;
if (d1 < d2) {
   w1= d1.getDay();
   w2= d2.getDay();
} else {
   w2= d1.getDay();
   w1= d2.getDay();
}
if (w1 > w2) t-=2;
if ((w1 === 0 && w2 === 6) || w1 == 6) t--;
d = Math.abs((Math.floor(d/7)*5)+t);
hols.forEach(function(x) {if (between(d1,d2,x)) d--;} );
  return d;
}

console.log(busDayDiff(new Date(2016,0,10),new Date(), [new Date(2016,0,26),new Date(2016,3,12), new Date(2017,1,1)]));

The first tweo parameters are the start and end dates you want the business days between and the third parameter is an array of the public holidays that don’t fall on a Saturday or Sunday

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.