Skip to content

Instantly share code, notes, and snippets.

@blangenfeld
Created September 30, 2013 17:41
Show Gist options
  • Save blangenfeld/6767372 to your computer and use it in GitHub Desktop.
Save blangenfeld/6767372 to your computer and use it in GitHub Desktop.
AngularJS directives for formatting arrays of dates.
angular.module('kf.filters', []).
/**
* @ngdoc filter
* @name kf.filters:dates
* @function
*
* @description
* Passed an Array of Dates, returns a String joining them together with a delimiter. The dates
* themselves may be customized with an Angular `date` filter specifier.
*
* @param {Array} input The source Array of Dates.
* @param {string=} format Formatting rules (see Angular's `date` filter).
* @param {string=} delimiter Delimiter separating the dates. If not specified, ` - ` is used.
*/
filter('dates', function($filter) {
return function(input, format, delimiter) {
delimiter = delimiter || ', ';
return input.map(function(date) { return $filter('date')(date, format); }).join(delimiter);
};
}).
/**
* @ngdoc filter
* @name kf.filters:dateRange
* @function
*
* @description
* Passed an Array of Dates, returns a String joining the first and last Dates with a delimiter.
* The dates themselves may be customized with an Angular `date` filter specifier.
*
* @param {Array} input The source Array of Dates.
* @param {string=} format Formatting rules (see Angular's `date` filter).
* @param {string=} delimiter Delimiter separating the two dates. If not specified, ` - ` is used.
*/
filter('dateRange', function($filter) {
return function(input, format, delimiter) {
delimiter = delimiter || ' - ';
var ends = [], last = input.length - 1;
if(last >= 0) { ends.push(input[0]); }
if(last >= 1) { ends.push(input[last]); }
return $filter('dates')(ends, format, delimiter);
};
});
@blangenfeld
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment