web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

JavaScript – Business Days

Neil Parkhurst Profile Picture Neil Parkhurst 10,727 User Group Leader

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

Filed under: JavaScript Tagged: JavaScript

Comments

*This post is locked for comments