# Adding times

**URL:** <https://www.ruby-forum.com/t/adding-times/73405>\
**Category:** Ruby\
**Created:** [October 24, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/adding-times/73405 "2006-10-24T14:03:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![EBEBEB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f6c823/32.png) [@EBEBEB](https://www.ruby-forum.com/u/EBEBEB)\
**Post date:** [October 24, 2006, 2:03pm UTC](https://www.ruby-forum.com/t/adding-times/73405/1 "2006-10-24T14:03:18Z")

</div>

Ok, I try to use ruby for all sorts on-the-fly things as  
they come up so that I can practice & learn about it 🙂

So, I’m looking up Ruby in a Nutshell, and amazon says  
if I order in the next 10 hours and 40 minutes I can get  
it soon.

“Cool” I think … “I can use Ruby to do the time math”,  
and irb is my friend 🙂

However, I can’t get it to work. This is what I tried:

irb(main):001:0\> now=Time.now  
=\> Tue Oct 24 07:49:31 -0400 2006

irb(main):002:0\> hours = 10  
=\> 10

irb(main):003:0\> mins = 40  
=\> 40

irb(main):004:0\> total\_minutes = hours \* 60 + mins  
=\> 640

irb(main):005:0\> now  
=\> Tue Oct 24 07:49:31 -0400 2006

irb(main):006:0\> now.min  
=\> 49

irb(main):007:0\> now.min + total\_minutes  
=\> 689

irb(main):008:0\> now + total\_minutes  
=\> Tue Oct 24 08:00:11 -0400 2006

irb(main):009:0\> now  
=\> Tue Oct 24 07:49:31 -0400 2006

Is there an easy way to add specific hours and minutes to a  
given time? I have a feeling this is easy to do, I just don’t  
know how.

Thanks,  
eb

---

<div class="post-metadata">

**Author:** ![EBEBEB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f6c823/32.png) [@EBEBEB](https://www.ruby-forum.com/u/EBEBEB)\
**Post date:** [October 24, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/adding-times/73405/2 "2006-10-24T14:17:19Z")

</div>

> irb(main):008:0\> now + total\_minutes

The argument for Time#+ is the seconds to add.

Kalman

---

<div class="post-metadata">

**Author:** ![EBEBEB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f6c823/32.png) [@EBEBEB](https://www.ruby-forum.com/u/EBEBEB)\
**Post date:** [October 24, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/adding-times/73405/3 "2006-10-24T14:21:20Z")

</div>

Kalman N. wrote:

> > irb(main):008:0\> now + total\_minutes
> 
> The argument for Time#+ is the seconds to add.

Hi,

cool … so I was somewhat close. Multiplying my total\_mins  
by 60 to get seconds and then adding them seems to do the  
trick!

Thanks Kalman,

eb

---

<div class="post-metadata">

**Author:** ![EBEBEB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f6c823/32.png) [@EBEBEB](https://www.ruby-forum.com/u/EBEBEB)\
**Post date:** [October 24, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/adding-times/73405/4 "2006-10-24T14:23:24Z")

</div>

EB wrote:

> Is there an easy way to add specific hours and minutes to a  
> given time? I have a feeling this is easy to do, I just don’t  
> know how.

I’m not sure I understand exactly what you’re trying to do, but  
something like this:

class Fixnum  
def minutes  
self_60  
end  
def hours  
self_3600  
end  
end

Time.now + 7.minutes  
x = Time.now  
x += 13.hours + 17.minutes

Something like this can be found in Facets too.

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![EBEBEB](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f6c823/32.png) [@EBEBEB](https://www.ruby-forum.com/u/EBEBEB)\
**Post date:** [October 24, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/adding-times/73405/5 "2006-10-24T14:52:17Z")

</div>

Ola B. wrote:

> self_60  
> end  
> def hours  
> self_3600  
> end  
> end
> 
> Time.now + 7.minutes  
> x = Time.now  
> x += 13.hours + 17.minutes

Nice! Thanks!

> Something like this can be found in Facets too.

I had no idea about Ruby F.s, I just google for it, thanks for  
mentioning it.

There is so much to learn, and such little time!! (love learning though)

eb
