Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Angular JS Ago Filter
ng = angular.module 'filters', []
#Convert a date to an "ago" string
ng = ng.filter "ago", () -> (dt) ->
if not dt then return ''
agofn = (val,unit)-> return val + ' '+unit+(if val>0 then 's')+' ago'
msecs = (new Date()).getTime()-(new Date(dt)).getTime()
days = parseInt(msecs/(24*60*60*1000))
if days>0 then return agofn days, 'day'
hours = parseInt(msecs/(60*60*1000))
if hours>0 then return agofn hours, 'hour'
mins = parseInt(msecs/(60*1000))
if mins>0 then return agofn mins, 'minute'
secs = parseInt(msecs/1000)
return agofn secs,'second'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment