Skizit
Skizit

Reputation: 44852

Date counting in Ruby

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

Answers (2)

TuteC
TuteC

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

sepp2k
sepp2k

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

Related Questions