Jump to content

The ultimate community for Ruby on Rails developers.


Photo

date loop problem....

date loop

  • Please log in to reply
1 reply to this topic

#1 Peterlawn

Peterlawn

    Passenger

  • Members
  • 1 posts

Posted 04 July 2014 - 02:02 AM

i'm trying to make a date loop which from  2014-06-01 to 2014-07-02

 

so i tried this : 

date_from.upto(date_to) do |day|

and

(date_from..date_to).each do |day|

but both of them , stopped at 06-30 and throw an error......

 

finally , i use the following to avoid the error. I just want to ask.........is there any better solution ?

while(date_from <= date_to) do
           #do something

            if date_from == date_from.end_of_month
              date_from += 1.month
            else
              date_from += 1.day
            end
          end


#2 Ohm

Ohm

    Driver

  • Moderators
  • 410 posts
  • LocationCopenhagen

Posted 04 July 2014 - 05:28 AM

Hmm, trying this in IRB

require 'date'

date_from = Date.parse('2014-06-01')
date_to = Date.parse('2014-07-02')

(date_from..date_to).each do |day| 
  puts day 
end

works just fine, and prints all the dates. Could you be a bit more specific on the error?


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users