require 'active_support/core_ext'
class Day < Date
alias :succ :tomorrow
end
class Month < Date
alias :succ :next_month
end
class Year < Date
alias :succ :next_year
end
def get_arr_between(from, to, target='d')
from = Date.parse(from) if from.is_a?(String)
to = Date.parse(to) if to.is_a?(String)
format = case target
when 'd' then '%Y-%m-%d'
when 'm' then '%Y-%m'
when 'y' then '%Y'
end
from.step(to).collect { |d| d.strftime(format) }.compact.uniq
end
献丑。
具体用法很简单
get_arr_between(from, to)
get_arr_between(from, to, 'm')
get_arr_between(from, to, 'y')