How Do I Loop Through a Date Range in Reverse?

Posted by Russ Bradberry on Stack Overflow See other posts from Stack Overflow or by Russ Bradberry
Published on 2010-05-14T18:51:12Z Indexed on 2010/05/14 18:54 UTC
Read the original article Hit count: 373

Filed under:
|
|
|

I have a date range that I would like to be able to loop through in reverse. Give the following, how would I accomplish this, the standard Range operator doesn't seem t be working properly.

>> sd = Date.parse('2010-03-01')
=> Mon, 01 Mar 2010
>> ed = Date.parse('2010-03-05')
=> Fri, 05 Mar 2010
>> (sd..ed).to_a
=> [Mon, 01 Mar 2010, Tue, 02 Mar 2010, Wed, 03 Mar 2010, Thu, 04 Mar 2010, Fri, 05 Mar     2010]
>> (ed..sd).to_a
=> []

as you can see, the range operator works properly form start to end, but not from end to start.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails