Answer the question
In order to leave comments, you need to log in
How can I get the difference in months from two dates in Ruby?
For example, there are variables:
start_date = '01.09.2015'
finish_date = '01.02.2016'
Answer the question
In order to leave comments, you need to log in
I'll put in my 5 cents
require 'date'
start = Date.parse('2012-09-02')
finish = Date.parse('2013-11-02')
date_months = (start..finish).map {|d| Date.new(d.year, d.month) }.uniq
date_months.map {|d| d.strftime "%m/%Y" }
["09/2012", "10/2012", "11/2012", "12/2012", "01/2013", "02/2013", "03/2013", "04/2013", "05/2013", "06/2013", "07/2013", "08/2013", "09/2013", "10/2013", "11/2013"]
Easy:
require 'date'
start = Date.parse('2012-09-02')
finish = Date.parse('2013-11-30')
dates_array = (start..finish).map(&:to_s)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question