将时间转化成秒

  def to_seconds(str)
    time_arr = []
    time_arr = str.split(":").reverse
    sec = 0
    secs = [1, 60, 60*60, 60*60*24]
    time_arr.size.times do |i|
      sec += secs[i].to_i * time_arr[i].to_i
    end
    sec
  end

 

具体示例:

 

    puts to_seconds("01:02:03")  # => 3723

 

你可能感兴趣的:(时间)