Skip to content

Instantly share code, notes, and snippets.

@shirts
Created June 21, 2016 18:20
Show Gist options
  • Save shirts/b3b1c816ba0bfaea80360a3429a5858c to your computer and use it in GitHub Desktop.
Save shirts/b3b1c816ba0bfaea80360a3429a5858c to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'date'
class Date
def self.months_between(d1, d2)
months = []
start_date = Date.civil(d1.year, d1.month, 1)
end_date = Date.civil(d2.year, d2.month, 1)
raise ArgumentError unless d1 <= d2
while (start_date < end_date)
months << start_date
start_date = start_date >>1
end
months << end_date
end
end
# Date.months_between '2012-09-01'.to_date, Date.today.to_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment