Last active
August 29, 2015 14:15
-
-
Save saibotsivad/781b1215b145032c5536 to your computer and use it in GitHub Desktop.
An angular filter calculating the the days between two dates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('eds.daysBetweenDates', []).filter('daysBetweenDates', function() { | |
return function(startDate, endDate) { | |
if (startDate && endDate) { | |
var start = moment(startDate).hour(0).minute(0).second(0).millisecond(0); | |
var end = moment(endDate).hour(0).minute(0).second(0).millisecond(0).add('day', 1); | |
return moment(end).diff(start, 'days') | |
} else { | |
return 0; | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('filter', function() { | |
beforeEach(module('eds.daysBetweenDates')); | |
describe('daysBetweenDates', function() { | |
it('should be 1', inject(function(daysBetweenDatesFilter) { | |
var start = new Date(); | |
var end = new Date(); | |
expect(daysBetweenDatesFilter(start, end)).toEqual(1); | |
})); | |
it('should be 2', inject(function(daysBetweenDatesFilter) { | |
var start = moment().subtract(1, 'days').toDate(); | |
var end = new Date(); | |
expect(daysBetweenDatesFilter(start, end)).toEqual(2); | |
})); | |
it('should be 8', inject(function(daysBetweenDatesFilter) { | |
var start = new Date(); | |
var end = moment().add(1, 'weeks').toDate(); | |
expect(daysBetweenDatesFilter(start, end)).toEqual(8); | |
})); | |
it('should be 2', inject(function(daysBetweenDatesFilter) { | |
var start = moment('2012-01-01 01:00:00').toDate(); | |
var end = moment('2012-01-02 23:00:00').toDate(); | |
expect(daysBetweenDatesFilter(start, end)).toEqual(2); | |
})) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment