-
Notifications
You must be signed in to change notification settings - Fork 29
/
momentjs-business.js
55 lines (46 loc) · 1.43 KB
/
momentjs-business.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
/**
* momentjs-business.js
* businessDiff (mStartDate)
* businessAdd (numberOfDays)
*/
(function () {
var moment;
moment = (typeof require !== "undefined" && require !== null) &&
!require.amd ? require("moment") : this.moment;
moment.fn.businessDiff = function (param) {
param = moment(param);
var signal = param.unix() < this.unix()?1:-1;
var start = moment.min(param, this).clone();
var end = moment.max(param, this).clone();
var start_offset = start.day() - 7;
var end_offset = end.day();
var end_sunday = end.clone().subtract('d', end_offset);
var start_sunday = start.clone().subtract('d', start_offset);
var weeks = end_sunday.diff(start_sunday, 'days') / 7;
start_offset = Math.abs(start_offset);
if(start_offset == 7)
start_offset = 5;
else if(start_offset == 1)
start_offset = 0;
else
start_offset -= 2;
if(end_offset == 6)
end_offset--;
return signal * (weeks * 5 + start_offset + end_offset);
};
moment.fn.businessAdd = function (days) {
var signal = days<0?-1:1;
days = Math.abs(days);
var d = this.clone().add(Math.floor(days / 5) * 7 * signal, 'd');
var remaining = days % 5;
while(remaining){
d.add(signal, 'd');
if(d.day() !== 0 && d.day() !== 6)
remaining--;
}
return d;
};
moment.fn.businessSubtract = function(days){
return this.businessAdd(-days);
};
}).call(this);