# Find by Date Regardless of Time

**URL:** <https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691>\
**Category:** Rails\
**Created:** [April 27, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691 "2010-04-27T22:10:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 27, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691/1 "2010-04-27T22:10:50Z")

</div>

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])

–

* * *

Joshua S. Martin

CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),  
contains information that may be confidential, protected by the attorney  
client or other legal privileges, and or proprietary non public  
information.  
If you are not an intended recipient of this message or an authorized  
assistant to an intended recipient, please notify the sender by replying  
to  
this message and then delete it from your system. Use, dissemination,  
distribution, or reproduction of this message and or any of its  
attachments  
(if any) by unintended recipients is not authorized and may be unlawful.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 27, 2010, 10:21pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691/2 "2010-04-27T22:21:57Z")

</div>

On Apr 27, 9:07 pm, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 27, 2010, 10:27pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691/3 "2010-04-27T22:27:13Z")

</div>

Ah, I see. Thanks!

On Tue, Apr 27, 2010 at 4:20 PM, Frederick C. \<  
removed\_email\_address@domain.invalid\> wrote:

> Fred
> 
> > ## contains information that may be confidential, protected by the attorney
> 
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

* * *

Joshua S. Martin

CONFIDENTIALITY NOTE: This e-mail message, including any attachment(s),  
contains information that may be confidential, protected by the attorney  
client or other legal privileges, and or proprietary non public  
information.  
If you are not an intended recipient of this message or an authorized  
assistant to an intended recipient, please notify the sender by replying  
to  
this message and then delete it from your system. Use, dissemination,  
distribution, or reproduction of this message and or any of its  
attachments  
(if any) by unintended recipients is not authorized and may be unlawful.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 27, 2010, 11:05pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691/4 "2010-04-27T23:05:51Z")

</div>

On Apr 27, 2010, at 4:24 PM, Joshua M. wrote:

> 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 B.  
[http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

> **[Launch Scout Custom Software Development & Software Consulting](https://launchscout.com/)**
>
> We solve digital problems through custom software development and design for the web—apps, mobile, desktop and devices.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 28, 2010, 7:04pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691/5 "2010-04-28T19:04:08Z")

</div>

Rob B. 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’)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 29, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/find-by-date-regardless-of-time/187691/6 "2010-04-29T18:05:53Z")

</div>

On Tue, Apr 27, 2010 at 4:07 PM, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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])

You might want to look at

> **[GitHub - radar/by\_star: Lets you find ActiveRecord + Mongoid objects by year,...](https://github.com/radar/by_star)**
>
> Lets you find ActiveRecord + Mongoid objects by year, month, fortnight, week and more! - GitHub - radar/by\_star: Lets you find ActiveRecord + Mongoid objects by year, month, fortnight, week and more!

Just discovered this in the past few days.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
