Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through Dates in Rails

I'm writing a method that iterates using dates given a start and end date, and I need to the date within the loop. Something like this:

start_date = 2013/03/12
end_date = 2013/11/20

# from start_date to end_date do |f|
  puts date_this_iteration_is_processing
end

Can someone please help me with this?

I've read these questions, but they didn't indicate how to determine the date that the given iteration is on.

How to Loop through Months in Ruby on Rails Iterating the dates in ruby on rails

like image 259
yellowreign Avatar asked Mar 26 '13 22:03

yellowreign


1 Answers

is this what you are looking for? use the range operator ..

start_date = Date.parse('2013-03-12')
end_date = Date.parse('2013-11-20')

(start_date..end_date).each do |day|
  puts day
end
like image 119
house9 Avatar answered Oct 29 '22 05:10

house9