Ruby/Rails - How to convert seconds to time?

后端 未结 4 1724
既然无缘
既然无缘 2021-02-19 16:52

I need to perform the following conversion:

0     -> 12.00AM
1800  -> 12.30AM
3600  -> 01.00AM
...
82800 -> 11.00PM
84600 -> 11.30PM
4条回答
  •  深忆病人
    2021-02-19 17:44

    Not sure if this is better than

    (Time.local(1,1,1) + 82800).strftime("%I:%M%p")
    
    
    def hour_minutes(seconds)
      Time.at(seconds).utc.strftime("%I:%M%p")
    end
    
    
    irb(main):022:0> [0, 1800, 3600, 82800, 84600].each { |s| puts "#{s} -> #{hour_minutes(s)}"}
    0 -> 12:00AM
    1800 -> 12:30AM
    3600 -> 01:00AM
    82800 -> 11:00PM
    84600 -> 11:30PM
    

    Stephan

提交回复
热议问题