Last active
September 16, 2015 08:31
-
-
Save lnaia/76eaa336f520ca70a409 to your computer and use it in GitHub Desktop.
Angular JS time-ago.filter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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