Skip to content

Instantly share code, notes, and snippets.

@eristoddle
Forked from balazsbohonyi/businessDays.js
Created August 22, 2016 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eristoddle/b95de0c3ec46bd0125a091113811aff5 to your computer and use it in GitHub Desktop.
Save eristoddle/b95de0c3ec46bd0125a091113811aff5 to your computer and use it in GitHub Desktop.
Adding/Subtracting Business Days in Javascript (extends the native Date Javascript object)
Number.prototype.mod = function(n) {
return ((this%n)+n)%n;
}
// support for adding/subtracting business days for Javascript dates
Date.prototype.addBusinessDays = function(days) {
days = parseInt(days);
var wks = Math.floor(days/5);
var dys = days.mod(5);
var dy = this.getDay();
if (dy === 6 && dys > -1) {
if (dys === 0) {dys-=2; dy+=2;}
dys++; dy -= 6;}
if (dy === 0 && dys < 1) {
if (dys === 0) {dys+=2; dy-=2;}
dys--; dy += 6;}
if (dy + dys > 5) dys += 2;
if (dy + dys < 1) dys -= 2;
this.setDate(this.getDate()+wks*7+dys);
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment