RAM PRATHIP
RAM PRATHIP

Reputation: 13

Finding the months between two dates in rails

I can currently set a time range like so:

start_date: "2018-09-11"
end_date: "2018-11-19"

How can I do this for start to end of months? Examples:

time_range = ["2018-09-11".."2018-09-30"]
time_range = ["2018-10-01".."2018-10-31"]
time_range = ["2018-11-01".."2018-11-19"]

Upvotes: 1

Views: 4273

Answers (4)

JBlanco
JBlanco

Reputation: 526

With the information provided by the OP, this is what I understand he is looking for. Given a set range for example:

 time_range = "2018-09-11".."2018-09-19"

 new_range_min = time_range.min.to_date.beginning_of_month
 new_range_max = time_range.max.to_date.end_of_month
 new_range = new_range_min..new_range_max

Upvotes: 0

Cary Swoveland
Cary Swoveland

Reputation: 110745

This is a pure Ruby solution, but I believe (though I don't know Rails) it can be simplified slightly by replacing my methods first_day_of_month and first_day_of_month with Rails methods beginning_of_month and end_of_month, respectively. I designed the method for efficiency over simplicity.

require 'date'

DATE_FMT = "%Y-%m-%d"

def date_ranges(start_date_str, end_date_str)
  start_date = Date.strptime(start_date_str, DATE_FMT)
  end_date   = Date.strptime(end_date_str, DATE_FMT)
  return [start_date_str..end_date_str] if
    [start_date.year, start_date.month] == [end_date.year, end_date.month]
  d = start_date
  ranges = [start_date_str..last_day_of_month(d)]
  loop do
    d = d >> 1
    break if [d.year, d.month] == [end_date.year, end_date.month]
    ranges << (first_day_of_month(d)..last_day_of_month(d))
  end
  ranges << (first_day_of_month(d)..end_date_str)
end

def first_day_of_month(d)
  (d - d.day + 1).strftime(DATE_FMT)
end

def last_day_of_month(d)
  ((d >> 1)-d.day).strftime(DATE_FMT)
end

date_ranges("2018-09-11", "2019-02-11")
  #=> ["2018-09-11".."2018-09-30", "2018-10-01".."2018-10-31",
  #    "2018-11-01".."2018-11-30", "2018-12-01".."2018-12-31",
  #    "2019-01-01".."2019-01-31", "2019-02-01".."2019-02-11"]

date_ranges("2018-09-08", "2018-09-23")
  #=> ["2018-09-08".."2018-09-23"]

Upvotes: 0

Leonardo da Rosa
Leonardo da Rosa

Reputation: 31

I do not know if I understand very well what you asked, but I'll try to help you.

The Date class has several methods that will help you to work with dates. Date < Object

Examples

my_date_range_array = [Date.today.beginning_of_year..Date.today.end_of_year]

my_date_time_range_array = [Time.now.beginning_of_year..Time.now.end_of_year]

my_date_range_array = [6.months.ago..Date.today]


YourModel.where date: Date.today.beginning_of_month..Date.today

YourModel.where date: 6.months.ago..Date.today

If you need every single date in the range, you can use something like this:

(Date.today.beginning_of_year..Date.today.end_of_year).map{ |date| date }

I hope that my answer helps you

Upvotes: 0

Ursus
Ursus

Reputation: 30071

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

Upvotes: 2

Related Questions