Angular Date Filter with moment js


(function() {
    'use strict';

    angular.module('myApp', [])
        .filter('moment', [
            function() {
                return function(date, method) {
                    var momented = moment(date);
                    return momented[method].apply(momented, Array.prototype.slice.call(arguments, 2));
                };
            }
        ])
        .controller('myCtrl', function($scope) {
            $scope.date = new Date();
        });
}());

<div>{{ date | moment:'format':'dddd, MMMM Do YYYY, h:mm:ss a' }}</div>
<div>{{ date | moment:'fromNow' }}</div>
<div>{{ date | moment:'fromNow':true }}</div>
<div>{{ date | moment:'from':[2007, 0, 28] }}</div>
<div>{{ date | moment:'calendar' }}</div>
<div>{{ date | moment:'calendar':[2007, 0, 28] }}</div>
<div>{{ date | moment:'unix' }}</div>
<div>{{ date | moment:'daysInMonth' }}</div>
<div>{{ date | moment:'toDate' }}</div>
<div>{{ date | moment:'toArray' }}</div>
<div>{{ date | moment:'toJSON' }}</div>
<div>{{ date | moment:'toISOString' }}</div>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s