# Comparing Ruby time with SQL time

**URL:** <https://www.ruby-forum.com/t/comparing-ruby-time-with-sql-time/87247>\
**Category:** Rails\
**Created:** [March 1, 2007, 3:59am UTC](https://www.ruby-forum.com/t/comparing-ruby-time-with-sql-time/87247 "2007-03-01T03:59:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [March 1, 2007, 3:59am UTC](https://www.ruby-forum.com/t/comparing-ruby-time-with-sql-time/87247/1 "2007-03-01T03:59:12Z")

</div>

Hi everyone,

Quick question: I want to grab all rows in a database posted less than  
24 hours ago. Here’s what I have in my controller:

def stats  
now = Time.now  
recipe\_yesterday = now.yesterday  
@recipes = Recipe.find(:all, :conditions =\> “date\_added \>  
#{recipe\_yesterday}”)  
end

I think there’s a problem because the formatting of the time generated  
by Time is different than how time is stored in my database. Is there a  
way to reconcile the two?

Thanks!

Dave

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [March 1, 2007, 4:10am UTC](https://www.ruby-forum.com/t/comparing-ruby-time-with-sql-time/87247/2 "2007-03-01T04:10:09Z")

</div>

Hi –

On Thu, 1 Mar 2007, Dave A. wrote:

> #{recipe\_yesterday}")  
> end
> 
> I think there’s a problem because the formatting of the time generated  
> by Time is different than how time is stored in my database. Is there a  
> way to reconcile the two?

Try this:

Recipe.find(:all,  
:conditions =\> “date\_added \>  
‘#{recipe\_yesterday.to\_s(:db)}’”)

There may be a more concise way to do it that I’m not remembering, but  
that should give you comparable formats.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [March 1, 2007, 4:24am UTC](https://www.ruby-forum.com/t/comparing-ruby-time-with-sql-time/87247/3 "2007-03-01T04:24:09Z")

</div>

Hi Dave,

Dave A. wrote:

> I want to grab all rows in a database posted less than  
> 24 hours ago. Here’s what I have in my controller:
> 
> def stats  
> now = Time.now  
> recipe\_yesterday = now.yesterday  
> @recipes = Recipe.find(:all, :conditions =\> “date\_added \>  
> #{recipe\_yesterday}”)  
> end

Try:  
@recipes = Recipe.find(:all, :conditions =\> [“date\_added \> ?”,  
recipe\_yesterday])

hth,  
Bill
