-
-
Save cmmartin/341b017194bac09ffa1a to your computer and use it in GitHub Desktop.
// 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); | |
}; | |
}); |
Thanks!
throws error "Uncaught SyntaxError: Unexpected reserved word ....moment.js:7" does it need namespacing or I missed something.
@tenzopro Not sure what's going on for you. If you throw it in a fiddle or plunkr, I'll take a look. Here it is working in a fiddle...
Awesome, thanks
This works great, but I've noticed that when using it with 'fromNow' the text doesn't update as time passes, presumably since the underlying model value isn't changing. Is there some trick to force this?
This seems to do the trick:
module.filter('momentFromNow', function () {
var fromNow = function(input) {
return moment(input).fromNow();
};
fromNow.$stateful = true;
return fromNow;
});
Very useful, I have been trying to adapt your gist for Spanish lenguage without success yet. 😞
Thanks a lot!
Really useful, thanks a lot !
Just have added this filter to my project. Thanks very much bro :-)
@uecasm so you created another filter just for the fromNow method.
is that the best way to go about it.
please I need more ideas on this
TypeError: Cannot read property 'slice' of undefined
at form.js:274
at fn (eval at compile (angular.js:15351), :4:215)
at regularInterceptedExpression (angular.js:16459)
at expressionInputWatch (angular.js:16354)
at Scope.$digest (angular.js:18002)
at Scope.$apply (angular.js:18280)
at tick (angular.js:13193)
<3
❤️
thanks.