Skip to content

Instantly share code, notes, and snippets.

@ikouchiha47
Created September 21, 2016 10:47
Show Gist options
  • Save ikouchiha47/11131dc24424bc474fca54c4a5b9da9b to your computer and use it in GitHub Desktop.
Save ikouchiha47/11131dc24424bc474fca54c4a5b9da9b to your computer and use it in GitHub Desktop.
def gen_dates(arr)
arr.reduce([]) do |acc, v|
if acc.last
d = Date.parse(v)
diff = (Date.parse(acc.last) - d).to_i
if diff == 1
acc.push(v)
else
acc.concat((1..(diff-1)).map { |i| (d + i).strftime("%Y-%m-%d") })
acc.push(v)
end
else
acc.push(v)
end
end
end
# output
gen_dates(["2016-09-21", "2016-09-20", "2016-09-18", "2016-09-12"])
["2016-09-21", "2016-09-20", "2016-09-19", "2016-09-18", "2016-09-13", "2016-09-14", "2016-09-15", "2016-09-16", "2016-09-17", "2016-09-12"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment