Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ruby Method To Get Seconds From Colon-separated Time String

DZone's Guide to

Ruby Method To Get Seconds From Colon-separated Time String

·
Free Resource
// converts colon-separated time to seconds
// "01:02:03" => 3723


def colon_separated_to_i(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
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}