I’m calculating the duration of an event by simply doing ‘end_time -
start_time’. This outputs the time in seconds.
However, i’d like this to be displayed in hours, minutes and seconds in
HH:mm:ss format.
Is this possible?
I’m calculating the duration of an event by simply doing ‘end_time -
start_time’. This outputs the time in seconds.
However, i’d like this to be displayed in hours, minutes and seconds in
HH:mm:ss format.
Is this possible?
i don’t know that there is a specific rails method to do that.
however, something like this should do the trick.
def hms(secs)
h, secs = secs.divmod(3600)
m, s = secs.divmod(60)
h = h.to_s.rjust(2, “0”)
m = m.to_s.rjust(2, “0”)
s = s.round.to_s.rjust(2, “0”)
“#{h}:#{m}:#{s}”
end
put that in your application_helper.rb file
<%= hms(@object.duration_in_secs) -%>
Paul N. wrote:
I’m calculating the duration of an event by simply doing ‘end_time -
start_time’. This outputs the time in seconds.However, i’d like this to be displayed in hours, minutes and seconds in
HH:mm:ss format.Is this possible?
I’m not aware of any Rails methods for this, so here is my
implementation:
def format_time
seconds = time % 60
minutes = (time / 60) % 60
hours = (time / 3600) % 60
“#{hours}::#{minutes}::#{seconds}”
end
–
Cheers,
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs