Skip to content

Instantly share code, notes, and snippets.

@markhuge
Created July 19, 2013 11:22
Show Gist options
  • Select an option

  • Save markhuge/6038456 to your computer and use it in GitHub Desktop.

Select an option

Save markhuge/6038456 to your computer and use it in GitHub Desktop.
Handlebars Helper - Time Since <date>
Handlebars.registerHelper 'timesince', (date) ->
seconds = Math.floor((new Date() - new Date(date)) / 1000)
interval = Math.floor seconds / 31536000
if interval > 1
return new Handlebars.SafeString interval + " years"
interval = Math.floor seconds / 2592000
if interval > 1
return new Handlebars.SafeString interval + " months"
interval = Math.floor seconds / 86400
if interval > 1
return new Handlebars.SafeString interval + " days"
interval = Math.floor seconds / 3600
if interval > 1
return new Handlebars.SafeString interval + " hours"
interval = Math.floor seconds / 60
if interval > 1
return new Handlebars.SafeString interval + " minutes"
return new Handlebars.SafeString Math.floor(seconds) + " seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment