-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (52 loc) · 1.69 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
'use strict';
if (typeof require === 'function') {
const moment = require('moment');
const locale = moment().localeData();
const defaultWorkingWeekdays = [1, 2, 3, 4, 5];
const workingWeekdays = locale._workingWeekdays || defaultWorkingWeekdays;
moment.fn.businessDiff = function (param) {
const d1 = this.clone();
const d2 = param.clone();
const start = d1 < d2 ? d1 : d2;
const end = d2 > d1 ? d2 : d1;
const startDateMoment = moment(start.format('YYYY-MM-DD'), 'YYYY-MM-DD');
const endDateMoment = moment(end.format('YYYY-MM-DD'), 'YYYY-MM-DD');
const stdDiff = endDateMoment.diff(startDateMoment, 'days');
let numOfDays = 0;
if (!endDateMoment.isSameOrBefore(startDateMoment)) {
numOfDays = startDateMoment.countBusinessDaysInDaysFromDate(stdDiff);
}
return numOfDays;
};
moment.fn.isBusinessDay = function () {
return workingWeekdays.includes(+moment(this).format('E'));
};
moment.fn.countBusinessDaysInDaysFromDate = function (days) {
let date = this, counter = 0;
for (let i = 0; i < days; i++) {
if (workingWeekdays.includes(+date.format('E'))) {
counter++;
}
date.add(1, 'd')
}
return counter;
};
moment.fn.businessAdd = function (number) {
var day = this.clone();
var signal = number < 0 ? -1 : 1;
var remaining = Math.abs(number);
while (remaining) {
day.add(signal, 'days');
if (day.isBusinessDay()) {
remaining--;
}
}
return day;
};
moment.fn.businessSubtract = function (number, period) {
return this.businessAdd(-number, period);
};
if (typeof module != 'undefined' && module.exports) {
module.exports = moment;
}
}