views:

1086

answers:

1

How do I add/subtract/etc. time values in Ruby? For example, how would I add the following times?

00:00:59 + 00:01:43 + 00:20:15 = ?

+5  A: 
>> 59.seconds + (1.minute + 43.seconds) + (20.minutes + 15.seconds)
=> 1377 seconds

Look at ActiveSupport's Time extensions: http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Numeric/Time.html

EDIT: For clarity's sake, ActiveSupport is part of Rails, not Ruby's core libraries.

Matt Darby