Created
August 8, 2013 18:55
-
-
Save balazsbohonyi/6187568 to your computer and use it in GitHub Desktop.
Adding/Subtracting Business Days in Javascript (extends the native Date Javascript object)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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