# Problem with date

**URL:** <https://www.ruby-forum.com/t/problem-with-date/154964>\
**Category:** Rails\
**Created:** [December 5, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/problem-with-date/154964 "2008-12-05T17:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [December 5, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/problem-with-date/154964/1 "2008-12-05T17:18:21Z")

</div>

Hi, first and foremost sorry for my english. I have a pbroblem trying to  
built a search by data.

My view is a simple text\_field:

\<% form\_tag ({:action =\>‘buscadata’},{:class =\> ‘lgn’}) do%\>   
\<%= stylesheet\_link\_tag “show” %\>

# Busqueda por fechas de LGN

**\<%='Introduzca Fecha inicial (DD/MM/AA)'%\>**  
 \<%= text\_field\_tag :dia, params[:dia\_ini]%\> \<%= text\_field\_tag :mes, params[:mes\_ini]%\> \<%= text\_field\_tag :any, params[:any\_ini]%\>

**\<%='Introduzca Fecha Final (DD/MM/AA)'%\>**  
 \<%= text\_field\_tag :dia, params[:dia\_fin]%\> \<%= text\_field\_tag :mes, params[:mes\_fin]%\> \<%= text\_field\_tag :any, params[:any\_fin]%\>

\<%= submit\_tag “Buscar entre las fechas seleccionadas”, :name=\>nil%\>

\<%end%\>

* * *

In the controller I search @lgns created between the two dates:

def buscadata  
date1  
=parse\_date("#{params[:any\_ini]}/#{params[:mes\_ini]}/#{params[:dia\_ini]}")  
date2  
=parse\_date("#{params[:any\_fin]}/#{params[:mes\_fin]}/#{params[:dia\_fin]}")  
if date1 and date2  
@lgns=Lgn.find(:all, :conditions =\> [‘created\_at \>=? and  
created\_at \<=?’, date1, date2 ])  
end  
end  
######################################################################################################  
private

def parse\_date(str, format=’%Y/%m/%d’)  
begin  
date = Date.parse(str, format) #strptime  
return date  
rescue ArgumentError  
return false  
end  
end

* * *

Finally I show @lgns find, buscadata view it’s the same that index view.

But I have a problem, @lgns it’s a nil object. Somebody can help me?? I  
cant see whats wrong ☹

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [December 5, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/problem-with-date/154964/2 "2008-12-05T17:19:21Z")

</div>

Sorry, the search is by date no by data

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [December 5, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/problem-with-date/154964/3 "2008-12-05T17:58:39Z")

</div>

Now it shows me the view but with all the objects ☹

My new code:

CONTROLLER:  
def buscadata  
date1  
=parse\_date("#{params[:dia\_ini]}/#{params[:mes\_ini]}/#{params[:any\_ini]}")  
date2  
=parse\_date("#{params[:dia\_fin]}/#{params[:mes\_fin]}/#{params[:any\_fin]}")  
if date1 and date2  
@lgns=Lgn.find(:all, :conditions =\> [‘created\_at \>=? and  
created\_at \<=?’, date1, date2 ])  
end  
end  
######################################################################################################  
private

def parse\_date(str, format=’%Y/%m/%d’)  
begin  
date =str.to\_date #Date.parse(str, format)  
#strptime  
return date  
rescue ArgumentError  
return false  
end  
end

the view when I introduce the date:  
\<% form\_tag ({:action =\>‘buscadata’},{:class =\> ‘lgn’}) do%\>   
\<%= stylesheet\_link\_tag “show” %\>

# Busqueda por fechas de LGN

**\<%='Introduzca Fecha inicial (DD/MM/AA)'%\>**  
 \<%= text\_field\_tag :dia\_ini, params[:dia\_ini]%\> \<%= text\_field\_tag :mes\_ini, params[:mes\_ini]%\> \<%= text\_field\_tag :any\_ini, params[:any\_ini]%\>

**\<%='Introduzca Fecha Final (DD/MM/AA)'%\>**  
 \<%= text\_field\_tag :dia\_fin, params[:dia\_fin]%\> \<%= text\_field\_tag :mes\_fin, params[:mes\_fin]%\> \<%= text\_field\_tag :any\_fin, params[:any\_fin]%\>

\<%= submit\_tag “Buscar entre las fechas seleccionadas”, :name=\>nil%\>

\<%end%\>

the view when I show the results it’s like the index view

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [December 5, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/problem-with-date/154964/4 "2008-12-05T18:03:54Z")

</div>

On 5 Dec 2008, at 16:58, Jose vicente Ribera pellicer wrote:

> Now it shows me the view but with all the objects ☹

Have you checked whether your parse\_date function is creating the  
correct dates?

Fred

---

<div class="post-metadata">

**Author:** ![chimorito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@chimorito](https://www.ruby-forum.com/u/chimorito)\
**Post date:** [December 5, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/problem-with-date/154964/5 "2008-12-05T18:21:07Z")

</div>

Solved…(mounth/day/year).to\_date it’s the correct form. Thanks a lot
