-
Notifications
You must be signed in to change notification settings - Fork 3
/
README
41 lines (33 loc) · 1.8 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
A JavaScript function to add n business days to a Date instance.
Several versions exist in the addBusinessDays.js file. Choose the right
one for your needs.
Version 1: Modifies the Date in place.
var d = new Date(2009, 10, 14);
addBusinessDays(d, 1); // 2009-10-15
addBusinessDays(d, 2); // 2009-10-19 because d + 3 falls on the weekend
addBusinessDays(d, 3); // 2009-10-22 because d is modified in each call
Version 2: Preserves the input Date. Returns a new Date.
var d = new Date(2009, 10, 14);
var d2 = addBusinessDays(d, 1); // 2009-10-15
var d3 = addBusinessDays(d, 2); // 2009-10-16 because d is unmodified
var d4 = addBusinessDays(d, 3); // 2009-10-19 because d + 3 falls on the weekend
Version 3: Modifies the Date. Adjusts for holidays
d = new Date(2009, 12, 22);
addBusinessDays(d, 1); // 2009-12-23
addBusinessDays(d, 2); // 2009-12-28 because d + 3 falls on a holiday and d + 4
// falls on the weekend
Version 4: Preserves the input Date. Adjusts for holidays. Returns a new Date.
d = new Date(2009, 12, 22);
var d2 = addBusinessDays(d, 1); // 2009-12-23
var d3 = addBusinessDays(d, 2); // 2009-12-24 this can be configured to be
// a holiday if desired
var d4 = addBusinessDays(d, 3); // 2009-12-28 because d + 3 is a holiday, d + 4
// falls on the weekend
* NOTE: The algo assumes business days are between Monday and Friday. Patches welcome to account for alternate work weeks.
The holiday mapping is in an object attached to the addBusinessDays function
named 'holidays'. Assign the holidays for the desired years in the format:
addBusinessDays.holidays[YEAR][MONTHDAY] = true;
e.g.
addBusinessDays.holidays['2009']['1126'] = true;
or for holidays that always fall on the same date
addBusinessDays.holidays.all['1225'] = true;