Skip to content

Instantly share code, notes, and snippets.

@cmmartin
Last active July 31, 2019 09:01
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cmmartin/341b017194bac09ffa1a to your computer and use it in GitHub Desktop.
Save cmmartin/341b017194bac09ffa1a to your computer and use it in GitHub Desktop.
A generic Moment.js date filter for Angular.js
// REQUIRES:
// moment.js - http://momentjs.com/
// USAGE:
// {{ someDate | moment: [any moment function] : [param1] : [param2] : [param n]
// EXAMPLES:
// {{ someDate | moment: 'format': 'MMM DD, YYYY' }}
// {{ someDate | moment: 'fromNow' }}
// To call multiple moment functions, you can chain them.
// For example, this converts to UTC and then formats...
// {{ someDate | moment: 'utc' | moment: 'format': 'MMM DD, YYYY' }}
angular.module('myModule').filter('moment', function () {
return function (input, momentFn /*, param1, param2, ...param n */) {
var args = Array.prototype.slice.call(arguments, 2),
momentObj = moment(input);
return momentObj[momentFn].apply(momentObj, args);
};
});
@KevinLedoyen
Copy link

<3

@lacivert
Copy link

❤️

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