Skip to content

Instantly share code, notes, and snippets.

@lnaia
Last active September 16, 2015 08:31
Show Gist options
  • Save lnaia/76eaa336f520ca70a409 to your computer and use it in GitHub Desktop.
Save lnaia/76eaa336f520ca70a409 to your computer and use it in GitHub Desktop.
Angular JS time-ago.filter.js
angular.module('app')
.filter('timeAgo', function() {
return function(input) {
var res = '',
day = 24 * 60 * 60 * 1000,
hour = 60 * 60 * 1000,
minute = 60 * 1000,
second = 1000,
difference = new Date() - new Date(input);
if (difference > day) {
res = Math.floor(difference / day);
res = res + ' ' + (res > 1 ? 'days' : 'day');
} else if (difference > hour) {
res = Math.floor(difference / hour);
res = res + ' ' + (res > 1 ? 'hours' : 'hour');
} else if (difference > minute) {
res = Math.floor(difference / minute);
res = res + ' ' + (res > 1 ? 'minutes' : 'minute');
} else {
res = Math.floor(difference / second);
res = res + ' ' + (res > 1 ? 'seconds' : 'second');
}
return res + ' ago';
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment