Trouble using moment.js date formatting lib

This is how I do it:

In your html file’s <head> reference the moment library:

<script src="vendor/moment.js"></script>

Then create a filter like to the below:

angular.module('my.filters').filter('friendlyDate', function() {
    	return function(date) {
    		return moment(date).format('MMMM Do YYYY, h:mm:ss a');
    	};
});

Now anywhere you need a formatted date, you can simply do it this way:

<div data-ng-if="thing.date">{{thing.date | friendlyDate}}</div>

Just make sure your filter is visible to your backing controller, hope this helps.

1 Like