-
Notifications
You must be signed in to change notification settings - Fork 3
/
addBusinessDays.js
108 lines (89 loc) · 2.94 KB
/
addBusinessDays.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* addBusinessDays( d, plusDays )
*
* Choose the right implementation for your needs. If you need more than one
* make sure you change the name(s) of any additional versions.
*/
///////////////////////////////
// modifies the input Date //
///////////////////////////////
function addBusinessDays(d,n) {
var day = d.getDay();
d.setDate(
d.getDate() + n +
(day === 6 ? 2 : +!day) +
(Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
}
///////////////////////////////////////
// creates and modifies a new Date //
///////////////////////////////////////
function addBusinessDays(d,n) {
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;
}
// Used by both holiday aware versions below
// TODO: make this more abstract to support dynamically calculated holidays?
addBusinessDays.holidays = {
all: {
'0101': 1, // or true
// TODO: Fill in common (US) holidays
'1225': 1
},
2009: {
// Same format for things like Thanksgiving or other holidays that
// fall on e.g. the 3rd Monday of April in a given year
},
2010: {
// And so on for other years. Manual update required :(
}
};
// Used to test a Date instance against the holiday map above
addBusinessDays.isHoliday = function (d) {
function zeroPad(n) {
n |= 0;
return (n < 10 ? '0' : '') + n;
}
var day = zeroPad(d.getMonth() + 1) + zeroPad(d.getDate());
return addBusinessDays.holidays.all[day] ||
addBusinessDays.holidays[d.getFullYear()][day];
};
//////////////////////////////////////////////////////
// modifies the input Date, accounts for holidays //
//////////////////////////////////////////////////////
// @TODO: only accounts for the destination day being a holiday. It doesn't
// adjust for holidays between start and end date
function addBusinessDays(d,n) {
var day = d.getDay();
d.setDate(
d.getDate() + n +
(day === 6 ? 2 : +!day) +
(Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
if (addBusinessDays.isHoliday(d)) {
addBusinessDays(d,1);
}
}
//////////////////////////////////////////////////////////////
// creates and modifies a new Date, accounts for holidays //
//////////////////////////////////////////////////////////////
// @TODO: only accounts for the destination day being a holiday. It doesn't
// adjust for holidays between start and end date
var addBusinessDays = (function () {
function add(d,n) {
var day = d.getDay();
d.setDate(
d.getDate() + n +
(day === 6 ? 2 : +!day) +
(Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
if (addBusinessDays.isHoliday(d)) {
add(d,1);
}
}
return function (d,n) {
return add(new Date(d.getTime()),n);
}
})();