Skip to content

Instantly share code, notes, and snippets.

@amio
Created April 25, 2014 08:54
Show Gist options
  • Save amio/11282675 to your computer and use it in GitHub Desktop.
Save amio/11282675 to your computer and use it in GitHub Desktop.
Angular filter for transforming a date to 'xxx days ago'
'use strict';
angular.module('Utils', [])
/**
* Transform a date to 'xxx days ago'
*/
.filter('ago', function () {
return function (input) {
var date = new Date(input);
var seconds = Math.floor((new Date() - date) / 1000);
var intervals = {
'year': 31536000,
'month': 2592000,
'day': 86400,
'hour': 3600,
'minute': 60,
'second': 1
};
var counter;
for (var intv in intervals) {
counter = Math.floor(seconds / intervals[intv]);
if (counter > 0) {
return counter + ' ' + intv + 's ago';
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment