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

后端 未结 13 760
一整个雨季
一整个雨季 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条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 00:22

    Shout out to @joshuapinter who gave the best answer (in the form of a comment).

    Use the drop-in replacement dotiw gem to gain more control over the accuracy of the output to suit different needs:

    https://github.com/radar/distance_of_time_in_words

    Sample view code:

    %label
      Logoff after:
      - expire_in = distance_of_time_in_words(Time.now, Time.now + user.custom_timeout.minutes, :only => [:minutes, :hours, :days])
      = expire_in
    

    Resulting in something like this:

    Logoff after: 1 day, 13 hours, and 20 minutes
    

提交回复
热议问题