how to convert 270921sec into days + hours + minutes + sec ? (ruby)

前端 未结 9 621
不知归路
不知归路 2020-12-02 06:09

I have a number of seconds. Let\'s say 270921. How can I display that number saying it is xx days, yy hours, zz minutes, ww seconds?

9条回答
  •  心在旅途
    2020-12-02 06:15

    Needed a break. Golfed this up:

    s = 270921
    dhms = [60,60,24].reduce([s]) { |m,o| m.unshift(m.shift.divmod(o)).flatten }
    # => [3, 3, 15, 21]
    

提交回复
热议问题