JavaScript – Business Days

Recently I had a small challenge to calculate a date “n” working days from today. This short post will show you the code I used.

In my example, a working day was defined as Monday to Friday.

I wasn’t worried about business closures or bank holidays. I simply needed a date to act as a target to complete work by.

The code snippet below was the function I finally used;

function BusinessDays(d, n) {
 // *** d = a date 
 // *** n = number of working days to increment by
  d = new Date(d.getTime());
  var day = d.getDay();
  d.setDate(d.getDate() + n + (day === 6 ? 2 : +!day) + (Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
 return d;
} // End function

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s