data retrieve for the current date

30 views
Skip to first unread message

amvis

unread,
Feb 3, 2012, 12:16:26 PM2/3/12
to rubyonra...@googlegroups.com

My database(PostgreSQL) have some amount of data, so i need to retrieve the data for current date only.But my doubt is how to compare the today date with created_at field. Now am trying so many ways to find that. 

Is this correct way  where created_at BETWEEN '2012-01-16 00:00:00.00000' AND '2012-01-17 00:00:00.00000'...?


Thank you
vishnu

amvis

unread,
Feb 3, 2012, 1:12:56 PM2/3/12
to rubyonra...@googlegroups.com
one query got it from guides.rubyonrails.org

Robert Walker

unread,
Feb 3, 2012, 1:12:43 PM2/3/12
to rubyonra...@googlegroups.com
amvis wrote in post #1043961:

> My database(PostgreSQL) have some amount of data, so i need to retrieve
> the
> data for current date only.But my doubt is how to compare the today date
> with created_at field. Now am trying so many ways to find that.
>
> Is this correct way * where created_at BETWEEN '2012-01-16
> 00:00:00.00000'
> AND '2012-01-17 00:00:00.00000'...?*

Answer by example:

$ rails console
1.9.3-p0 :001 > today = Date.today
=> Fri, 03 Feb 2012
1.9.3-p0 :002 > tomorrow = Date.today + 1.day
=> Sat, 04 Feb 2012
1.9.3-p0 :003 > orders = Order.where(:created_at => today...tomorrow)
Order Load (0.7ms) SELECT "orders".* FROM "orders" WHERE
("orders"."created_at" >= '2012-02-03' AND "orders"."created_at" <
'2012-02-04')

--
Posted via http://www.ruby-forum.com/.

amvis

unread,
Feb 3, 2012, 2:44:41 PM2/3/12
to rubyonra...@googlegroups.com
Thanks..



Philip Hallstrom

unread,
Feb 3, 2012, 2:58:09 PM2/3/12
to rubyonra...@googlegroups.com

Be sure to check your timezones...

Date.today does not honor the timezone set within Rails...

You might want to use Time.zone.now.to_date instead...

-philip

Reply all
Reply to author
Forward
0 new messages