# Question on spliting Only date

**URL:** <https://www.ruby-forum.com/t/question-on-spliting-only-date/179039>\
**Category:** Rails\
**Created:** [November 16, 2009, 10:01am UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039 "2009-11-16T10:01:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 16, 2009, 10:01am UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/1 "2009-11-16T10:01:54Z")

</div>

Hello

i would like to get only date from this ActiveSupport::TimeWithZone  
class 2009-11-16 11:00:51.

i want to get only 2009-11-16

since i want to use like below

@groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \<=  
?”,@from\_date,@to\_date])

Is there any other ways i can think of ?

Thanks in advance for your reply

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 16, 2009, 11:02am UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/2 "2009-11-16T11:02:18Z")

</div>

Hi

You can type cast from timestamp to date .For your example it like (I  
am changing date to created\_at for readability You can change it)

> @groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \<=  
> ?”,@from\_date,@to\_date])

@groups = GroupsInfo.find(:all, :conditions=\>["CAST(created\_at as date)

> = ? and CAST(created\_at as date) \<= ?",@from\_date,@to\_date])

Sijo

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 16, 2009, 11:03am UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/3 "2009-11-16T11:03:22Z")

</div>

@groups = GroupsInfo.find(:all, :conditions=\>["CAST(created\_at as date)

> = ? and CAST(created\_at as date) \<= ?",@from\_date,@to\_date])

Sijo

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 16, 2009, 12:50pm UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/4 "2009-11-16T12:50:52Z")

</div>

Sijo k g wrote:

> @groups = GroupsInfo.find(:all, :conditions=\>["CAST(created\_at as date)
> 
> > = ? and CAST(created\_at as date) \<= ?",@from\_date,@to\_date])
> 
> Sijo

Thank u all it works fine.

Thanks a lot for your reply

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 17, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/5 "2009-11-17T15:09:20Z")

</div>

Newb N. wrote:

> Sijo k g wrote:
> 
> > @groups = GroupsInfo.find(:all, :conditions=\>["CAST(created\_at as date)
> > 
> > > = ? and CAST(created\_at as date) \<= ?",@from\_date,@to\_date])

FYI: If you don’t want to have to rely on database functions, I believe,  
you could do the type conversion in Ruby:

@groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \<= ?”,  
@from\_date.to\_date ,@to\_date.to\_date])

I am assuming @from\_date and @to\_date are instances of Time or DateTime.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 17, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/6 "2009-11-17T15:15:40Z")

</div>

Robert W. wrote:

> Newb N. wrote:
> 
> > Sijo k g wrote:
> > 
> > > @groups = GroupsInfo.find(:all, :conditions=\>["CAST(created\_at as date)
> > > 
> > > > = ? and CAST(created\_at as date) \<= ?",@from\_date,@to\_date])
> 
> FYI: If you don’t want to have to rely on database functions, I believe,  
> you could do the type conversion in Ruby:
> 
> @groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \<= ?”,  
> @from\_date.to\_date ,@to\_date.to\_date])

That won’t work. It will only affect the values in the WHERE clause.

> I am assuming @from\_date and @to\_date are instances of Time or DateTime.

## Best,

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

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 17, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/7 "2009-11-17T15:26:20Z")

</div>

Marnen Laibow-Koser wrote:

> Robert W. wrote:
> 
> > Newb N. wrote:
> > 
> > > Sijo k g wrote:
> > > 
> > > > @groups = GroupsInfo.find(:all, :conditions=\>["CAST(created\_at as date)
> > > > 
> > > > > = ? and CAST(created\_at as date) \<= ?",@from\_date,@to\_date])
> > 
> > FYI: If you don’t want to have to rely on database functions, I believe,  
> > you could do the type conversion in Ruby:
> > 
> > @groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \<= ?”,  
> > @from\_date.to\_date ,@to\_date.to\_date])
> 
> That won’t work. It will only affect the values in the WHERE clause.

Disregard. I just reread the code and realized that it will work.

## Best,

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

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 17, 2009, 3:22pm UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/8 "2009-11-17T15:22:36Z")

</div>

Marnen Laibow-Koser wrote:

> Robert W. wrote:
> 
> > FYI: If you don’t want to have to rely on database functions, I believe,  
> > you could do the type conversion in Ruby:
> > 
> > @groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \<= ?”,  
> > @from\_date.to\_date ,@to\_date.to\_date])
> 
> That won’t work. It will only affect the values in the WHERE clause.

Yep. I just thought about that myself. Was about to post a correction.

Wouldn’t an alternate fix be to make the condition inclusive on the low  
end and exclusive on the high end?

@groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \< ?”,  
@from\_date.to\_date ,@to\_date.to\_date])

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [November 17, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/question-on-spliting-only-date/179039/9 "2009-11-17T20:22:45Z")

</div>

Robert W. wrote:

> @groups = GroupsInfo.find(:all, :conditions=\>[“date \>= ? and date \< ?”,  
> @from\_date.to\_date ,@to\_date.to\_date])

I also should have mentioned that @to\_date should be set to the day  
after your ending target date:

@to\_date.to\_date + 1.day

One final note/suggestion…

It is also possible to use the hash form with a range.

Example:  
from\_date = Time.now.to\_date - 10.days # 10 days ago  
to\_date = Time.now.to\_date + 1.day # midnight one day after target  
@posts = Post.find(:all, :conditions =\> { :published\_at =\>  
from\_date…to\_date })

SELECT \* FROM “posts” WHERE (“posts”.“published\_at” \>= ‘2009-11-07’ AND  
“posts”.“published\_at” \< ‘2009-11-18’)

Notice use of exclusive range (x…y), which would include all of  
2009-11-17 up to but not including midnight of 2009-11-18.

I only mention all this because I tend to avoid database specific  
implementation when possible/practical. However, there are definitely  
practical uses for database specific implementations.

I don’t know if there are any performance considerations to be taken  
into account in this particular case. I suppose one would be weighing  
database CAST+date range test performance against datetime range  
testing. My guess would be that the later would perform slightly better,  
but it’s difficult to know without benchmarking.
