ruby - Rails - Get the time difference in hours, minutes and seconds -


i'm looking idiomatic way time passed since given date in hours, minutes , seconds.

if given date 2013-10-25 23:55:00 , current date 2013-10-27 20:55:09, returning value should 45:03:09. time_difference , time_diff gems won't work requirement.

you can try this:

def time_diff(start_time, end_time)   seconds_diff = (start_time - end_time).to_i.abs    hours = seconds_diff / 3600   seconds_diff -= hours * 3600    minutes = seconds_diff / 60   seconds_diff -= minutes * 60    seconds = seconds_diff    "#{hours.to_s.rjust(2, '0')}:#{minutes.to_s.rjust(2, '0')}:#{seconds.to_s.rjust(2, '0')}" end 

and use it:

time_diff(time.now, time.now-2.days-3.hours-4.minutes-5.seconds) # => "51:04:04" 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -