Created
August 16, 2012 21:20
-
-
Save adamrunner/3373744 to your computer and use it in GitHub Desktop.
Calculate Difference in time in Days, Hours, Minutes, Seconds
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
def display_time(total_seconds) | |
total_seconds = total_seconds.to_i | |
days = total_seconds / 86400 | |
hours = (total_seconds / 3600) - (days * 24) | |
minutes = (total_seconds / 60) - (hours * 60) - (days * 1440) | |
seconds = total_seconds % 60 | |
display = '' | |
display_concat = '' | |
if days > 0 | |
display = display + display_concat + "#{days}d" | |
display_concat = ' ' | |
end | |
if hours > 0 || display.length > 0 | |
display = display + display_concat + "#{hours}h" | |
display_concat = ' ' | |
end | |
if minutes > 0 || display.length > 0 | |
display = display + display_concat + "#{minutes}m" | |
display_concat = ' ' | |
end | |
display = display + display_concat + "#{seconds}s" | |
display | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! This problem comes up often. I solved it in SQL awhile back.