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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Nichols 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, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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) -%>
On 7/23/07, Paul Nichols
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
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?
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---