Convert duration to hours:minutes:seconds (or similar) in Rails 3 or Ruby

后端 未结 13 873
一整个雨季
一整个雨季 2021-01-29 23:52

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

13条回答
  •  独厮守ぢ
    2021-01-30 00:20

    I guess you could do also something like:

    (Time.mktime(0)+3600).strftime("%H:%M:%S")
    

    To format it as you wish.

    BTW, originally I thought of using Time.at() but seems that EPOCH time on my Ubuntu is Thu Jan 01 01:00:00 +0100 1970 and not 00:00:00 hours as I expected, and therefore if I do:

    Time.at(3600).strftime("%H:%M:%S")
    

    Gives me 1 hour more than wanted.

提交回复
热议问题