Rails Calculate Date Range in Months

Rails calculate date range in months

Subtracting one Date or DateTime from another will yield the number of days as a fraction, but this can be evaluated as a Float or Fixnum as required.

For instance:

(Date.today - Date.today.advance(:months => -3)).to_f
# => 89.0

There were 89.0 days between today and the same calendar date three months ago. If you work this using 30-day months, or 30.4375 as they are on average, you end up with 2.92 months elapsed between then and now, or rounded up to the nearest integer, 3.

If you want to compute the precise number of calendar months, that is trickier, but can be done.

Finding the months between two dates in rails

I'm not sure what's exactly your desired outcome but, given start date and end date as Date objects, you can perform

(start_date..end_date).to_a.group_by(&:month).values

and at the end what you get is a three element array, and each element contains an array with all the dates in that range for a month

Rails, counting months in date range, between date ranges

Well I'm not 100% sure of how your code is set up but I would have just done something like this:

if financial_year_2014.include?(scorecard.begin_date..scorecard.end_date) || financial_year_2015.include?(scorecard.begin_date..scorecard.end_date)
#calculate based on 2014/2015 year only
else
#calculate based on split between the years
range_one = scorecard.begin_date..Date.new(2015,6,30)
range_two = Date.new(2015,6,30)..scorecard.end_date
#use new ranges to calculate
end

Find number of months between two Dates in Ruby on Rails

(date2.year * 12 + date2.month) - (date1.year * 12 + date1.month)

more info at http://www.ruby-forum.com/topic/72120

Rails calculate Date duration from start_date and end_date

To avoid the rational number, you could use a inclusive range and count the number of days via:

(start_date..end_date).count

Unfortunately, count actually traverses the range instead of just calculating the difference.

You could calculate the difference yourself using jd – the Julian day number:

end_date.jd - start_date.jd + 1

Or combine both of the above into:

(start_date.jd..end_date.jd).count

Here, count calculates the difference, because it is an integer range.

Note that the range-based solutions don't allow negative results because ranges are always ascending in Ruby.

get no of months, years between two dates in ruby

I'd calculate the difference in months (be aware that we ignore day differences here) and then calculate the number of years by dividing that number by 12:

##
# Calculates the difference in years and month between two dates
# Returns an array [year, month]
def date_diff(date1,date2)
month = (date2.year * 12 + date2.month) - (date1.year * 12 + date1.month)
month.divmod(12)
end

date_diff date1, date4 #=> [0, 4]
date_diff date1, date2 #=> [0, 0]
date_diff date1, date3 #=> [0, 1]
date_diff date1, date5 #=> [1, 3]

Convert date range to array of weeks and months

I can think of as below :

require 'date'

months = (Date.parse("2014-01-30")..Date.parse("2014-03-27")).group_by(&:month).map { |_,v| v.first.end_of_month.to_s }
# => ["2014-01-31", "2014-02-28", "2014-03-31"]

weeks = (Date.parse("2014-01-30")..Date.parse("2014-03-27")).select(&:sunday?).map(&:to_s)
# => ["2014-02-02",
# "2014-02-09",
# "2014-02-16",
# "2014-02-23",
# "2014-03-02",
# "2014-03-09",
# "2014-03-16",
# "2014-03-23"]

Ruby/Rails how to iterate months over a DateTime range?

Use groupdate gem. For example (modified example from the docs):

visible_products = Product.where("created_at > ?", 1.week.ago).group_by_day

# {
# 2015-07-29 00:00:00 UTC => 50,
# 2013-07-30 00:00:00 UTC => 100,
# 2013-08-02 00:00:00 UTC => 34
# }

Also, this will be much faster, because your grouping/counting will be done by database itself, without the need to pass all the records via Product.all call to your Rails code, and without the need to create ActiveRecord object for each one (even irrelevant).



Related Topics



Leave a reply



Submit