Last active
December 23, 2015 14:09
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://github.com/StephenOTT/add_missing_dates_ruby for full code sample and updated changes