Ruby: Convert time to seconds? -
how can convert time 10:30 seconds? there sort of built in ruby function handle that?
basically trying figure out number of seconds midnight (00:00) specific time in day (such 10:30, or 18:45).
you can use datetime#parse turn string datetime object, , multiply hour 3600 , minute 60 number of seconds:
require 'date'  # datetime.parse throws argumenterror if can't parse string if dt = datetime.parse("10:30") rescue false    seconds = dt.hour * 3600 + dt.min * 60 #=> 37800 end   as jleedev pointed out in comments, use time#seconds_since_midnight if have activesupport:
require 'active_support' time.parse("10:30").seconds_since_midnight #=> 37800.0      
Comments
Post a Comment