Ruby (with Rails) convert a string of time into seconds?
Posted
by
Ryan
on Stack Overflow
See other posts from Stack Overflow
or by Ryan
Published on 2011-01-07T08:49:15Z
Indexed on
2011/01/07
8:54 UTC
Read the original article
Hit count: 149
So, I've got a string of time... something along the lines of
'4 hours'
'48 hours'
'3 days'
'15 minutes'
I would like to convert those all into seconds. For '4 hours'
, this works fine
Time.parse('4 hours').to_i - Time.prase('0 hours').to_i
=> 14400 # 4 hours in seconds, yay
However, this doesn't work for 48 hours (outside of range error). It also does not work for 3 days (no information error), etc.
Is there a simple way to convert these strings into seconds?
© Stack Overflow or respective owner