Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
};
});
@dvidsilva

This comment has been minimized.

Copy link

dvidsilva commented Dec 15, 2014

This was really useful, thanks!

@MSavameri

This comment has been minimized.

Copy link

MSavameri commented Feb 21, 2015

thanks.

@modder2

This comment has been minimized.

Copy link

modder2 commented Jun 15, 2015

Thanks!

@tenzopro

This comment has been minimized.

Copy link

tenzopro commented Jul 7, 2015

throws error "Uncaught SyntaxError: Unexpected reserved word ....moment.js:7" does it need namespacing or I missed something.

@cmmartin

This comment has been minimized.

Copy link
Owner Author

cmmartin commented Jul 23, 2015

@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...

http://jsfiddle.net/cmmartin/f08Lxhba/

@ekstremedia

This comment has been minimized.

Copy link

ekstremedia commented Aug 10, 2015

Awesome, thanks

@uecasm

This comment has been minimized.

Copy link

uecasm commented Dec 7, 2015

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;
});
@CarMoreno

This comment has been minimized.

Copy link

CarMoreno commented Nov 11, 2016

Very useful, I have been trying to adapt your gist for Spanish lenguage without success yet. 😞

@wenksi

This comment has been minimized.

Copy link

wenksi commented Nov 24, 2016

Thanks a lot!

@guillaumebarranco

This comment has been minimized.

Copy link

guillaumebarranco commented Jan 23, 2017

Really useful, thanks a lot !

@nhuttrieukiem

This comment has been minimized.

Copy link

nhuttrieukiem commented Mar 18, 2017

Just have added this filter to my project. Thanks very much bro :-)

@radiumrasheed

This comment has been minimized.

Copy link

radiumrasheed commented Mar 25, 2017

@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

@viniciogomez89

This comment has been minimized.

Copy link

viniciogomez89 commented Apr 26, 2017

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)

@KevinLedoyen

This comment has been minimized.

Copy link

KevinLedoyen commented Apr 27, 2017

<3

@lacivert

This comment has been minimized.

Copy link

lacivert commented Oct 27, 2017

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.