Ruby method to get the months of quarters a given date belongs to

10,979

Solution 1

You can define a function for that to accept the date as argument and return the quarter

def current_quarter_months(date)
  quarters = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
  quarters[(date.month - 1) / 3]
end

The function will return array based on the the value of the quarter the date belongs to.

Solution 2

You can get the quarter from any date by doing this:

quarter = (((Date.today.month - 1) / 3) + 1).to_i

Or even shorter:

quarter = (Date.today.month / 3.0).ceil

Solution 3

You can do the following:

m = date.beginning_of_quarter.month
"#{m},#{m+1},#{m+2}"

as in

>> date=Date.parse "27-02-2011"
=> Sun, 27 Feb 2011  
>> m = date.beginning_of_quarter.month
=> 1
>> "#{m},#{m+1},#{m+2}"
=> "1,2,3"

Solution 4

For ones who have come here by searching "ruby date quarter". You can easily extend Date class with quarters:

class Date
  def quarter
    case self.month
    when 1,2,3
      return 1
    when 4,5,6
      return 2
    when 7,8,9
      return 3
    when 10,11,12
      return 4
    end
  end
end

Usage example:

Date.parse("1 jan").quarter # -> 1
Date.parse("1 apr").quarter # -> 2
Date.parse("1 jul").quarter # -> 3
Date.parse("1 oct").quarter # -> 4
Date.parse("31 dec").quarter # -> 4

Solution 5

The way your question is worded you can just use the #month

"then the result i must get is April, May,June as string or int like 4,5,6 for April to June."

April is month 4 so

d = Date.parse('2014-04-01')
 => Tue, 01 Apr 2014
d.month
=> 4

If you really want the quarter, you can open up the date class and add your own quarter method

class Date
  def quarter
    (month / 3.0).ceil
  end
end

Example Usage

d = Date.parse('2014-04-01')
  => Tue, 01 Apr 2014
d.quarter
  => 2
Share:
10,979
Mithun Sasidharan
Author by

Mithun Sasidharan

A Web Evangelist and FOSS Enthusiast.

Updated on June 08, 2022

Comments

  • Mithun Sasidharan
    Mithun Sasidharan almost 2 years

    I have a date and i want to find out the months of that particular quarter.How can i have this done in ruby in the easiest possible way? I mean if the date i give is 27-04-2011, then the result i must get is April, May,June as string or int like 4,5,6 for April to June.

  • jaydel
    jaydel over 12 years
    so this will return an array of three months...does this mean the caller then has to identify which quarter has those three months?
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : yes exaclty or else he can modify the function!! Stackoverflow is to help and not to code for poeple
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : Ok, i helped him in the way i could bro... The rest is upto him if he is satisfied with the answer or not. Since he has accepted the answer it's obvious !!
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : Please dont atleast let somobody down unnecessarily when you cant appreciate their efforts!!
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : His answer meets the questionaire's requirement completely!!
  • jaydel
    jaydel over 12 years
    Guys, he JUST edited the question. Let's not play revisionist history. Originally he asked for the quarter a date belonged to. However, seeing as how he changed his question I'll take off my -1 as now it fully answers his edited version of the question. And Mithun, I was not letting you down. As he originally asked you simply didn't ask the question. No offense or hostility intended at all...
  • jaydel
    jaydel over 12 years
    I removed my answer because it answered the original question not the changed version and as such is just signal to noise.
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : no bro not at all... what i thought was he wouldnt have accepted the answer if it didnt meet the standards.. Anyway thanks. Il make sure from next time that my answers are to the point :)
  • jaydel
    jaydel over 12 years
    @Mithun: in light of his change you answered perfectly :) Also it won't let me take away my -1. if you edit your answer (just some cosmetic change) I can then take the -1 away. you shouldn't be penalized :P
  • jaydel
    jaydel over 12 years
    @Seane Paul: here is the original question which was what we were discussing: "I have a date and i want to find out which quarter this date belongs to. How can i have this done in ruby in the easiest possible way??" I took that from the edit history directly.
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : Yes but the questionare wudnt have accepted the answer if it didnt meet up his requirement ryt bro !!
  • Pavling
    Pavling over 12 years
    If nothing else, the OP may have learnt a lesson in asking smart questions ;-) catb.org/~esr/faqs/smart-questions.html
  • jaydel
    jaydel over 12 years
    @Seane Paul: yep, I acknowledged that, too. But that was because the OP didn't ask his question and Mithun is a mindreader (that's kudos, not sarcasm). When we answer and evaluate answers we only have the OP's question to go on. It changed. I even deleted my answer that was suddenly incorrect :P
  • Mithun Sasidharan
    Mithun Sasidharan over 12 years
    @jaydel : I personally feel your answer was the most accurate one if all he wanted was just the qaurter!!!
  • jaydel
    jaydel over 12 years
    @Mithun: Thanks :) Enjoyed our exchange here. Good stuff
  • ecoding5
    ecoding5 about 8 years
    This works, but in Example Usage d.quarter should return 2 not 4
  • Jak S
    Jak S about 7 years
    This requires ActiveSupport
  • Tony Gaeta
    Tony Gaeta over 3 years
    Such a clean and perfect answer. Not sure why this hasn't been accepted.