Reputation: 44852
Is there a simple way in Ruby to count the number of days from YYYY-MM-DD to another YYYY-MM-DD and list them?
Upvotes: 1
Views: 148
Reputation: 4382
Date.parse('2010-01-01').upto(Date.parse('2010-01-31')) do |day|
puts day
end
Or:
(Date.parse('2010-01-01')..Date.parse('2010-01-31')).each do |day|
puts day
end
Upvotes: 5
Reputation: 370172
You can use Date.parse
to convert the strings to Date
objects and then simply use the two Date
objects in a range and call to_a
on that range. I.e.:
( Date.parse(string1) .. Date.parse(string2) ).to_a
Upvotes: 6