# Determine if a time is within a certain range

**URL:** <https://www.ruby-forum.com/t/determine-if-a-time-is-within-a-certain-range/171083>\
**Category:** Rails\
**Created:** [June 26, 2009, 6:32am UTC](https://www.ruby-forum.com/t/determine-if-a-time-is-within-a-certain-range/171083 "2009-06-26T06:32:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [June 26, 2009, 6:32am UTC](https://www.ruby-forum.com/t/determine-if-a-time-is-within-a-certain-range/171083/1 "2009-06-26T06:32:37Z")

</div>

Is there a way to determine if a timestamp is between 2 times of the  
day? For example, I need to know if a time is between 12:00 am and 4:00  
am. The field I’m looking at has a full timestamp (i.e. 2009-06-25  
19:58:37) The date is irrelevant, I just need to know if the time of day  
was in range.

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [June 26, 2009, 9:40am UTC](https://www.ruby-forum.com/t/determine-if-a-time-is-within-a-certain-range/171083/2 "2009-06-26T09:40:49Z")

</div>

yep

assuming the timestamp is returning a DateTime object, just call the  
hour method on that

eg.

@user.created\_at.hour would return an integer that is between 0 and 23

so

@user.created\_at.hour \> 0 and @user.created\_at.hour \< 4

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com/)**
>
> Ruby on Rails tips and tutorials
