Quantcast
Channel: Neil Parkhurst's Activities
Viewing all articles
Browse latest Browse all 1616

JavaScript – Business Days

$
0
0

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

Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>