Ruby: Convert time to seconds?

后端 未结 8 657
甜味超标
甜味超标 2020-12-28 08:16

How can I convert a time like 10:30 to seconds? Is there some sort of built in Ruby function to handle that?

Basically trying to figure out the number of seconds fro

8条回答
  •  隐瞒了意图╮
    2020-12-28 08:55

    require 'time'
    
    def seconds_since_midnight(time)
      Time.parse(time).hour * 3600 + Time.parse(time).min * 60 + Time.parse(time).sec
    end
    
    puts seconds_since_midnight("18:46")
    

    All great answers, this is what I ended up using.

提交回复
热议问题