# Get a hash/array with a range of dates?

**URL:** <https://www.ruby-forum.com/t/get-a-hash-array-with-a-range-of-dates/74292>\
**Category:** Rails\
**Created:** [November 3, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/get-a-hash-array-with-a-range-of-dates/74292 "2006-11-03T19:00:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://www.ruby-forum.com/u/sean)\
**Post date:** [November 3, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/get-a-hash-array-with-a-range-of-dates/74292/1 "2006-11-03T19:00:33Z")

</div>

I’m trying to build options for a select box that would output a range  
of dates between two dates that I specified. For example, if my two  
dates (both coming from datetime fields in a MySQL database) were:

October 25th, 2006  
October 29th, 2006

I’d like to output options like this:

(option value=“2006-10-25”)October 25th, 2006(/option)  
(option value=“2006-10-26”)October 26th, 2006(/option)  
(option value=“2006-10-27”)October 27th, 2006(/option)  
(option value=“2006-10-28”)October 28th, 2006(/option)  
(option value=“2006-10-29”)October 29th, 2006(/option)

I can’t figure out how to get the range of dates into a collection. I  
tried doing:  
@range\_of\_dates = @first\_date…@second\_date

But it didn’t work. Does anyone know how I can do this?

Thanks,  
Sean

---

<div class="post-metadata">

**Author:** ![sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://www.ruby-forum.com/u/sean)\
**Post date:** [November 3, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/get-a-hash-array-with-a-range-of-dates/74292/2 "2006-11-03T19:48:19Z")

</div>

Ruby DATE class implements the “upto” method, so assuming you can  
massage @first\_date and @second\_date as DATE objects, you should be able  
to do something like this (in your view):

\<% @first\_date.upto(@second\_date) do |date| %\>  
(option value="\<% date.to\_s %\>")\<% date.to\_s %\>(/option)  
\<% end %\>

Of course, you’ll need to massage the output of date.to\_s to get the  
formats that you want - strftime method does this for Time objects, not  
sure if it applies for Date objects.

c.

Sean wrote:

> I’m trying to build options for a select box that would output a range  
> of dates between two dates that I specified. For example, if my two  
> dates (both coming from datetime fields in a MySQL database) were:
> 
> October 25th, 2006  
> October 29th, 2006
> 
> I’d like to output options like this:
> 
> (option value=“2006-10-25”)October 25th, 2006(/option)  
> (option value=“2006-10-26”)October 26th, 2006(/option)  
> (option value=“2006-10-27”)October 27th, 2006(/option)  
> (option value=“2006-10-28”)October 28th, 2006(/option)  
> (option value=“2006-10-29”)October 29th, 2006(/option)
> 
> I can’t figure out how to get the range of dates into a collection. I  
> tried doing:  
> @range\_of\_dates = @first\_date…@second\_date
> 
> But it didn’t work. Does anyone know how I can do this?
> 
> Thanks,  
> Sean

---

<div class="post-metadata">

**Author:** ![sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://www.ruby-forum.com/u/sean)\
**Post date:** [November 3, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/get-a-hash-array-with-a-range-of-dates/74292/3 "2006-11-03T19:53:10Z")

</div>

Another option…

In your controller:

@range\_of\_dates = @first\_date.upto(@second\_date).map {|date| [date.to\_s,  
date.to\_s]}

In your view:

\<%= select(‘object’,‘method’,@range\_of\_dates) %\>

Again, you’ll want to get the formats of the date.to\_s correct in the  
first line of code. The first date.to\_s is what is used for the display,  
the second is what is used for the value.

c.
