# Adding time

**URL:** <https://www.ruby-forum.com/t/adding-time/94237>\
**Category:** Ruby\
**Created:** [April 26, 2006, 5:56am UTC](https://www.ruby-forum.com/t/adding-time/94237 "2006-04-26T05:56:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [April 26, 2006, 5:56am UTC](https://www.ruby-forum.com/t/adding-time/94237/1 "2006-04-26T05:56:11Z")

</div>

is it possable to do this:  
“$submit\_time = Time.now  
$rfpp = 3  
$rfpp.times do  
$submit\_time.hour += 1  
end”

I get error because you can’t add time like that, so what do I do?

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [April 26, 2006, 6:10am UTC](https://www.ruby-forum.com/t/adding-time/94237/2 "2006-04-26T06:10:52Z")

</div>

On Apr 26, 2006, at 5:56 AM, Mohammad wrote:

> is it possable to do this:  
> “$submit\_time = Time.now  
> $rfpp = 3  
> $rfpp.times do  
> $submit\_time.hour += 1  
> end”
> 
> I get error because you can’t add time like that, so what do I do?

First, you stop using global variables.

## $ ri Time#+ ----------------------------------------------------------------- Time#+ time + numeric =\> time

```
  Addition---Adds some number of seconds (possibly fractional) to
  time and returns that value as a new time.

     t = Time.now #=> Wed Apr 09 08:56:03 CDT 2003
     t + (60 * 60 * 24) #=> Thu Apr 10 08:56:03 CDT 2003

```

You could also check [ruby-doc.org](http://ruby-doc.org).

So:

```
 t = Time.now
 t += 3 * 60

```

– Daniel

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [April 26, 2006, 6:13am UTC](https://www.ruby-forum.com/t/adding-time/94237/3 "2006-04-26T06:13:53Z")

</div>

On Apr 26, 2006, at 6:07 AM, Daniel H. wrote:

> t += 3 \* 60

Oops, forgot another \* 60 in there… That’s what I get for posting  
at 6 AM.

– Daniel

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [April 26, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/adding-time/94237/4 "2006-04-26T15:44:07Z")

</div>

Logan C. wrote:

> On Apr 25, 2006, at 11:56 PM, Mohammad wrote:
> 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> one\_hour = 60 \* 60 # sixty seconds per minute, 60 minutes to an hour  
> old\_submit\_time = $submit\_time  
> $submit\_time = Time.at($submit\_time.to\_i + one\_hour)
> 
> if old\_submit\_time.hour + 1 == $submit\_time.hour  
> puts “Yay!”  
> end

Thanks Logan and Daniel

---

<div class="post-metadata">

**Author:** ![mohammad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mohammad](https://www.ruby-forum.com/u/mohammad)\
**Post date:** [April 26, 2006, 6:13am UTC](https://www.ruby-forum.com/t/adding-time/94237/5 "2006-04-26T06:13:54Z")

</div>

On Apr 25, 2006, at 11:56 PM, Mohammad wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

one\_hour = 60 \* 60 # sixty seconds per minute, 60 minutes to an hour  
old\_submit\_time = $submit\_time  
$submit\_time = Time.at($submit\_time.to\_i + one\_hour)

if old\_submit\_time.hour + 1 == $submit\_time.hour  
puts “Yay!”  
end
