# Get the beginning of the hour and the end of the hour

**URL:** <https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055>\
**Category:** Ruby\
**Created:** [September 15, 2010, 11:47pm UTC](https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055 "2010-09-15T23:47:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 15, 2010, 11:47pm UTC](https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055/1 "2010-09-15T23:47:20Z")

</div>

I would like to Get the beginning of the current hour and the end of the  
current hour in the regular time format Wed Sep 15 15:45:47 -0600 2010

I would like to get Wed Sep 15 15:00:00 -0600 2010 and Wed Sep 15  
15:59:59 -0600 2010

I have tried a few things and have search google for  
Time.now.beginning\_of\_hour and Time.now.end\_of\_hour, but no luck

Any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 16, 2010, 12:25am UTC](https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055/2 "2010-09-16T00:25:46Z")

</div>

On 9/15/2010 4:47 PM, Chris G. wrote:

> I would like to Get the beginning of the current hour and the end of the  
> current hour in the regular time format Wed Sep 15 15:45:47 -0600 2010
> 
> I would like to get Wed Sep 15 15:00:00 -0600 2010 and Wed Sep 15  
> 15:59:59 -0600 2010
> 
> I have tried a few things and have search google for  
> Time.now.beginning\_of\_hour and Time.now.end\_of\_hour, but no luck

Here’s a quickie for the beginning of the current hour:

Time.at(Time.now.to\_i / 3600 \* 3600)

and for the end of the current hour:

Time.at(Time.now.to\_i / 3600 \* 3600 + 59 \* 60 + 59)

You can simplify that last one a bit, but it’s a little more obvious  
what’s going on this way.

-Jeremy

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 16, 2010, 12:39am UTC](https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055/3 "2010-09-16T00:39:49Z")

</div>

On 10-09-15 02:47 PM, Chris G. wrote:

> Thanks

Time.local instantiation method?

t = Time.now  
bt = Time.local(t.year,t.mon,t.day,t.hour,0,0)  
et = Time.local(t.year,t.mon,t.day.d.hour,59,0)

xc

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 16, 2010, 2:34pm UTC](https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055/4 "2010-09-16T14:34:26Z")

</div>

On Thu, Sep 16, 2010 at 2:03 AM, Chris G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > xc
> 
> awesome…thats what I wanted

Not so fast! Is this really the end? Which hour does  
Time.local(t.year,t.mon,t.day.d.hour,59,17) belong to?

I would probably rather do

et = Time.local(t.year,t.mon,t.day.d.hour + 1,0,0)

and treat the hour as half open interval (i.e. beginning is included,  
end is excluded). Note that Time also has #usec!

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [September 16, 2010, 2:03am UTC](https://www.ruby-forum.com/t/get-the-beginning-of-the-hour-and-the-end-of-the-hour/195055/5 "2010-09-16T02:03:21Z")

</div>

Xeno C. / Eskimo North and Gmail wrote:

> On 10-09-15 02:47 PM, Chris G. wrote:
> 
> > Thanks
> 
> Time.local instantiation method?
> 
> t = Time.now  
> bt = Time.local(t.year,t.mon,t.day,t.hour,0,0)  
> et = Time.local(t.year,t.mon,t.day.d.hour,59,0)
> 
> xc

awesome…thats what I wanted
