I have a feeling there is a simple/built-in way to do this but I can\'t find it.
I have a duration (in seconds) in an integer and I want to display it in a friendly form
Add a custom method to the Integer
class. You can create a new file called pretty_duration.rb
in the initializers
folder:
class Integer
def pretty_duration
parse_string =
if self < 3600
'%M:%S'
else
'%H:%M:%S'
end
Time.at(self).utc.strftime(parse_string)
end
end
Call seconds.pretty_duration
anywhere in your project:
275.pretty_duration # => "04:35"
9823.pretty_duration # => "02:43:43"
This answer builds up on Lev Lukomsky's Code