Created
April 30, 2012 03:53
-
-
Save yocontra/2555359 to your computer and use it in GitHub Desktop.
Number of seconds to pretty date
This file contains hidden or 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
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