forked from eduolalo/moment-business-days
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (215 loc) · 5.43 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import moment from "moment"
moment.fn.isHoliday = function () {
var locale = this.localeData();
if (locale._holidays && locale._holidays.indexOf(this.format(locale._holidayFormat)) >= 0) {
return true;
}
if (locale.holiday) {
if (locale.holiday(this)) {
return true;
}
return false;
}
return false;
};
moment.fn.isBusinessDay = function () {
var locale = this.localeData();
var defaultWorkingWeekdays = [1, 2, 3, 4, 5];
var workingWeekdays = locale._workingWeekdays || defaultWorkingWeekdays;
if (locale._forcedBusinessDays && locale._forcedBusinessDays.indexOf(this.format(locale._forcedBusinessDaysFormat)) >= 0) {
return true;
}
if (this.isHoliday()) return false;
if (workingWeekdays.indexOf(this.day()) >= 0) return true;
return false;
};
moment.fn.businessDaysIntoMonth = function () {
if (!this.isValid()) {
return NaN;
}
var businessDay = this.clone();
var pointer = this.clone().startOf('month');
var end = this.clone().endOf('month');
var businessDaysIntoMonth = 0;
for(;;) {
if (pointer.isAfter(end, 'day')) {
break;
}
if (pointer.isBusinessDay()) {
businessDaysIntoMonth += 1;
}
if (businessDay.isSame(pointer, 'day')) {
break;
}
pointer.add(1, 'day')
}
return businessDaysIntoMonth;
};
moment.fn.businessDiff = function (param, relative) {
var d1 = this.clone();
var d2 = param.clone();
var positive = d1 >= d2;
var start = d1 < d2 ? d1 : d2;
var end = d2 > d1 ? d2 : d1;
var daysBetween = 0;
if (start.isSame(end, 'day')) {
return daysBetween;
}
while (start.isBefore(end, 'day')) {
if (start.isBusinessDay()) {
daysBetween += 1;
}
start.add(1, 'day');
}
if (!end.isBusinessDay()) {
daysBetween -= 1;
}
if (relative) {
return (positive ? daysBetween : -daysBetween);
}
return daysBetween;
};
moment.fn.businessAdd = function (number, period) {
var day = this.clone();
if (!day.isValid()) {
return day;
}
if (number < 0) {
number = Math.round(-1 * number) * -1;
} else {
number = Math.round(number);
}
var signal = number < 0 ? -1 : 1;
period = typeof period !== 'undefined' ? period : 'days';
var remaining = Math.abs(number);
while (remaining > 0) {
day.add(signal, period);
if (day.isBusinessDay()) {
remaining -= 1;
}
}
return day;
};
moment.fn.businessSubtract = function (number, period) {
return this.businessAdd(-number, period);
};
moment.fn.nextBusinessDay = function () {
var locale = this.localeData();
var loop = 1;
var defaultNextBusinessDayLimit = 7;
var limit = locale._nextBusinessDayLimit || defaultNextBusinessDayLimit;
while (loop < limit) {
if (this.add(1, 'day').isBusinessDay()) {
break;
}
loop += 1;
}
return this;
};
moment.fn.prevBusinessDay = function () {
var locale = this.localeData();
var loop = 1;
var defaultPrevBusinessDayLimit = 7;
var limit = locale._prevBusinessDayLimit || defaultPrevBusinessDayLimit;
while (loop < limit) {
if (this.subtract(1, 'day').isBusinessDay()) {
break;
}
loop += 1;
}
return this;
};
moment.fn.monthBusinessDays = function (partialEndDate) {
if (!this.isValid()) {
return [];
}
var day = this.clone().startOf('month');
var end = partialEndDate || this.clone().endOf('month');
var daysArr = [];
for(;;) {
if (day.isAfter(end)) {
break;
}
if (day.isBusinessDay()) {
daysArr.push(day.clone());
}
day.add(1, 'day');
}
return daysArr;
};
moment.fn.monthNaturalDays = function (fromToday) {
if (!this.isValid()) {
return [];
}
var day = fromToday ? this.clone() : this.clone().startOf('month');
var end = this.clone().endOf('month');
var daysArr = [];
var done = false;
while (!done) {
daysArr.push(day.clone());
if (end.diff(day.add(1, 'day')) < 0) {
done = true;
}
}
return daysArr;
};
moment.fn.monthBusinessWeeks = function (fromToday) {
fromToday = fromToday || false;
var startDate = fromToday ? this.clone() : this.clone().startOf('month');
return _getBusinessWeeks(this, null, startDate);
};
moment.fn.businessWeeksBetween = function (endDate) {
var startDate = this.clone();
return _getBusinessWeeks(this, endDate, startDate);
};
var _getBusinessWeeks = function (self, endDate, startDate) {
if (!self.isValid()) {
return [];
}
var day = startDate;
var end = endDate ? moment(endDate).clone() : self.clone().endOf('month');
var weeksArr = [];
var daysArr = [];
var done = false;
while (!done) {
if (day.isBusinessDay()) {
daysArr.push(day.clone());
}
if (day.day() === 5) {
weeksArr.push(daysArr);
daysArr = [];
}
if (end.diff(day.add(1, 'day')) < 0) {
if (daysArr.length < 5) {
weeksArr.push(daysArr);
}
done = true;
}
}
return weeksArr;
};
moment.fn.monthNaturalWeeks = function (fromToday) {
if (!this.isValid()) {
return [];
}
var day = fromToday ? this.clone() : this.clone().startOf('month');
var end = this.clone().endOf('month');
var weeksArr = [];
var daysArr = [];
var done = false;
while (!done) {
daysArr.push(day.clone());
if (day.day() === 6) {
weeksArr.push(daysArr);
daysArr = [];
}
if (end.diff(day.add(1, 'day')) < 0) {
if (daysArr.length < 7) {
weeksArr.push(daysArr);
}
done = true;
}
}
return weeksArr;
};
export default moment