Skip to content

Instantly share code, notes, and snippets.

@RajaJaganathan
Created September 19, 2015 08:50
Show Gist options
  • Save RajaJaganathan/7e931ac4e0ead9bce645 to your computer and use it in GitHub Desktop.
Save RajaJaganathan/7e931ac4e0ead9bce645 to your computer and use it in GitHub Desktop.
AngularJS : Generic moment filter that allow you to use any moment formatting method.
(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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment