Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StephenOTT/6646927 to your computer and use it in GitHub Desktop.
Save StephenOTT/6646927 to your computer and use it in GitHub Desktop.
Ruby Method for getting the Dates/months (this version is designed for getting months/First day of each month, and then parse out day later) between two dates. Example: Provide Date A and Date B and get month and Year(s) between two dates. Does not return Date A and Date B, only the dates between those dates. See: https://github.com/StephenOTT/a…
def addMissingMonths (datesHash)
count = 0
datesHash.keys.each do |x|
result = []
if x != datesHash.keys.last
(x+1.month).upto(datesHash.keys[count+1]-1.month) do |a|
# result << [a.month,a.year]
result << [a.at_beginning_of_month]
end
puts result.uniq.to_s
end
count += 1
end
end
Sample input for datesHas: {Sun, 01 Jan 2012=>58, Sat, 01 Sep 2012=>53, Sat, 01 Dec 2012=>58}
Output:
[[Wed, 01 Feb 2012], [Thu, 01 Mar 2012], [Sun, 01 Apr 2012], [Tue, 01 May 2012], [Fri, 01 Jun 2012], [Sun, 01 Jul 2012], [Wed, 01 Aug 2012]]
[[Mon, 01 Oct 2012], [Thu, 01 Nov 2012]]
@StephenOTT
Copy link
Author

See https://github.com/StephenOTT/add_missing_dates_ruby for full code sample and updated changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment