Skip to content

Commit

Permalink
Merge pull request #27 from kpihus/kpihus/bug/isBusinesDay
Browse files Browse the repository at this point in the history
Kpihus/bug/is busines doff
  • Loading branch information
eduolalo authored May 18, 2017
2 parents 5b87ea9 + 766d69e commit 4c6d1c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ moment.locale('us', {

### Methods:


**businessAdd(days)**

Will add just business days excluding Saturday and Sunday, return a moment date object:
Expand Down
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,26 @@ moment.fn.businessDaysIntoMonth = function () {
return businessDaysIntoMonth;
};

moment.fn.businessDiff = function(param) {
var end = this.clone();
var start = moment(param);
var daysBetween = 0;
moment.fn.businessDiff = function (param) {
var d1 = this.clone();
var d2 = param.clone();
var start = d1 < d2 ? d1 : d2;
var end = d2 > d1 ? d2 : d1;

if(start === end){
return daysBetween;
}
var daysBetween = 0;

while (start < end){
if(this.isBusinessDay(start)){
daysBetween++;
}
start = start.businessAdd(1)
if (start === end) {
return daysBetween;
}

while (start < end) {
if (start.isBusinessDay()) {
daysBetween++;
}
start.add(1, 'd')
}

return daysBetween;
return daysBetween;
};

moment.fn.businessAdd = function(number, period = 'days') {
Expand Down
21 changes: 16 additions & 5 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var resetLocale = function (done) {
};

describe('Moment Business Days', function () {

afterEach(resetLocale);
describe('.prevBusinessDay', function () {
describe('When today is Monday', function () {
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Moment Business Days', function () {
expect(moment('2012-12-25').isBusinessDay()).to.be.false;
expect(moment('2013-12-25').isBusinessDay()).to.be.false;
expect(moment('2014-12-25').isBusinessDay()).to.be.false;
expect(moment('2015-12-25').isBusinessDay()).to.be.false;
expect(moment('2015-12-25').isBusinessDay()).to.be.false;
expect(callCount).to.equal(4);
done();
});
Expand Down Expand Up @@ -103,10 +103,10 @@ describe('Moment Business Days', function () {
describe('.businessDaysIntoMonth', function () {

afterEach(resetLocale);

describe('On Wednesday, September 23rd 2015', function () {
it('should be 17 when there are no holidays', function (done) {

moment.updateLocale('us',{
workingWeekdays: null
});
Expand Down Expand Up @@ -171,13 +171,24 @@ describe('Moment Business Days', function () {
var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','MM-DD-YYYY'))
expect(diff).to.eql(5)
});
it('...and in reverse order', function(){
var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(moment('05-15-2017','MM-DD-YYYY'))
expect(diff).to.eql(5)
});
it('Should calculate nr of business days with custom workingdays', function(){
moment.updateLocale('us',{
workingWeekdays: [1,2,3,4,5,6]
});
var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','MM-DD-YYYY'))
expect(diff).to.eql(6)
})
});
it('Should calculate nr of business with all working days', function(){
moment.locale('us',{
workingWeekdays: [0,1,2,3,4,5,6]
});
var diff = moment('06-18-2017', 'MM-DD-YYYY').businessDiff(moment('05-18-2017'))
expect(diff).to.eql(31)
});
it('Should be zero days if start and end is same', function(){

var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017', 'MM-DD-YYYY'));
Expand Down

0 comments on commit 4c6d1c1

Please sign in to comment.