views:

29

answers:

1

Hi,

How can convert time in seconds to standard time format in rails.For example 1971 seconds should be displayed as 0:33:25

+1  A: 

There is no helper to that for you in rails. But it's quite easy to write your own:

def to_dot_time(s)
  h = s / 3600
  s -= h * 3600

  m = s / 60
  s -= m * 60

  [h, m, s].join(":")
end

And an example:

>> to_dot_time(1971)
=> "0:32:51"
Ariejan
Also not that the 0:33:25 in your question is wrong for 1971 seconds. Or did you use some other means of calculating that value?
Ariejan