Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created April 30, 2012 03:53
Show Gist options
  • Save yocontra/2555359 to your computer and use it in GitHub Desktop.
Save yocontra/2555359 to your computer and use it in GitHub Desktop.
Number of seconds to pretty date
prettySeconds = (secs) ->
days = Math.floor secs / 86400
hours = Math.floor (secs % 86400) / 3600
minutes = Math.floor ((secs % 86400) % 3600) / 60
seconds = ((secs % 86400) % 3600) % 60
out = ""
out += "#{days} days " if days > 0
out += "#{hours} hours " if hours > 0
out += "#{minutes} minutes" if minutes > 0
out += " #{seconds} seconds" if seconds > 0 and days <= 0
return out
###
Seconds are left out if there is more than one day. Remove this by deleting "and days <= 0"
prettySeconds(3471102) = "40 days 4 hours 11 minutes"
prettySeconds(347) = "5 minutes 47 seconds"
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment