Find by Date Regardless of Time

I have a datetime field in the database. How do I find records for a particular date regardless of the time of day?

The method I’m trying does not work… tasks.find(:all, :conditions => [“date == ?”, Date.today])

I have a datetime field in the database. How do I find records for a
particular date regardless of the time of day?

you need to search for records where that date column falls in the
appropriate range (ie midnight on that day until midnight on the
following day0

Fred

Ah, I see. Thanks!

Ah, I see. Thanks!

I have a datetime field in the database. How do I find records for a
particular date regardless of the time of day?

you need to search for records where that date column falls in the
appropriate range (ie midnight on that day until midnight on the
following day0

Fred

The method I’m trying does not work… tasks.find(:all, :conditions => [“date
== ?”, Date.today])

You might also be able to use date functions of your database engine to simplify the query (although depending on indexing, it may not be more performant).

tasks.find(:all, :conditions => [‘DATE(date) = ?’, Date.today])

Be careful with a range that entries that happen to fall exactly on midnight aren’t counted on both the preceding and following days.

-Rob

Rob Biedenharn

http://agileconsultingllc.com

Rob@AgileConsultingLLC.com

http://gaslightsoftware.com

Rob@GaslightSoftware.com

Rob Biedenharn wrote:

Be careful with a range that entries that happen to fall exactly on
midnight aren't counted on both the preceding and following days.

User.find(:all, :conditions => { :created_at =>
(Date.today)...(Date.today + 1) })

SELECT * FROM "users" WHERE ("users"."created_at" >= '2010-04-28' AND
"users"."created_at" < '2010-04-29')

You might want to look at

http://github.com/radar/by_star

Just discovered this in the past few days.