# Compare timestamps

**URL:** <https://www.ruby-forum.com/t/compare-timestamps/169974>\
**Category:** Rails\
**Created:** [June 10, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974 "2009-06-10T20:46:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 10, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/1 "2009-06-10T20:46:11Z")

</div>

Hi everyone,  
‘Person’ migration includes timestamps.  
I want to find every person created ‘today’.

Tried the command below, but I think it compares hour,minute and seconds  
too. I just want to compare day,month and year.  
Person.find(:all, :conditions =\> {:created\_at =\> Time.now})

any clues?

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 10, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/2 "2009-06-10T20:51:50Z")

</div>

Person.all( :conditions =\> [ ‘created\_at BETWEEN ? AND ?’,  
Time.now.at\_beginning\_of\_day, Time.now.at\_beginning\_of\_day + 1.day ] )

- 

Maurício Linhares  
[http://codeshooter.wordpress.com/](http://codeshooter.wordpress.com/) | [http://twitter.com/mauriciojr](http://twitter.com/mauriciojr)

On Wed, Jun 10, 2009 at 3:46 PM, Bruno

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 10, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/3 "2009-06-10T20:51:51Z")

</div>

On Jun 10, 7:46 pm, Bruno S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi everyone,  
> ‘Person’ migration includes timestamps.  
> I want to find every person created ‘today’.
> 
> Tried the command below, but I think it compares hour,minute and seconds  
> too. I just want to compare day,month and year.  
> Person.find(:all, :conditions =\> {:created\_at =\> Time.now})

You can’t use the hash form of :conditions for this. You need to ask  
the database for those records with a created\_at in a certain range  
(exactly what range will depend on your definiiton of today: past 24  
hours or since midnight)

Fred

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 10, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/4 "2009-06-10T21:01:11Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 10, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/5 "2009-06-10T21:27:53Z")

</div>

Okay,  
another issue:  
There are persons with the same names. I need to group them by their  
names and add their age(the ones that have same name).

## this table:

## NAME | AGE John | 23 Abe | 45 Amy | 40 John | 7 John | 5 Amy | 5

## would turn into

## NAME | AGE John | 35 Abe | 45 Amy | 45

Tried:  
Person.find(:all, :group =\> “name”, :conditions =\> [ ‘created\_at BETWEEN  
? AND ?’, Time.now.at\_beginning\_of\_day, Time.now.at\_beginning\_of\_day +  
1.day ])

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 11, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/6 "2009-06-11T21:11:46Z")

</div>

Thanks everyone!

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 11, 2009, 7:20pm UTC](https://www.ruby-forum.com/t/compare-timestamps/169974/7 "2009-06-11T19:20:40Z")

</div>

Bruno S. wrote:

> Okay,  
> another issue:  
> There are persons with the same names. I need to group them by their  
> names and add their age(the ones that have same name).  
> […]

This is best done with SQL aggregate functions, which ActiveRecord  
abstracts as Calculations. From memory, you probably want something  
like

Person.sum(:age, :group =\> :name)

…but check the docs, 'cause I may have messed up the syntax.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
