Skip to content

Instantly share code, notes, and snippets.

@npezza93
Created June 29, 2018 15:40
Show Gist options
  • Save npezza93/9a1b1839e4f09ae7a802659b6baf751d to your computer and use it in GitHub Desktop.
Save npezza93/9a1b1839e4f09ae7a802659b6baf751d to your computer and use it in GitHub Desktop.
months = [1,2,3,4,5,6,7,9,10,11,12].to_a.map { |month| Date.new(2018, month) }
months = months.map { |month| month..month.end_of_month }
months.inject([]) do |array, month|
if array.present? && array.last.last + 1.day == month.first
array.push(array.pop.first..month.last)
else
array.push(month)
end
end.map do |range|
[range.first.strftime("%b '%y"), range.last.beginning_of_month.strftime("%b '%y")].join(" - ")
end.join(" and ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment