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

6

13

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.

Gauss answered 7/12, 2011 at 11:45 Comment(6)
How exactly are you defining quarters?Ultraviolet
"4 quarter each year" is not "exact" :-/ Are your "quarters" comprised of full months, starting on the first day of the first and ending on the last day of the third month? Or do they follow seasons (and if so, how are you defining which date the seasons change on), or are they based on four spans of 91/92 days (and where are those spans demarked), or do they follow some arbitrary corporate financial period scheme?Groce
In practice a quarter begins on the first day of the first month of the quarter and ends on the last day of the last month of the quarter.Kliber
If you are talking about a fiscal quarter then it depends on a lot of factors. In us government runs oct to sept. in sweden there are four separate fiscal calendars that a business can use. Even simpler than that if you aren't talking fiscal, do you mean 365/4 or 12/4?Holocaine
12/4. I was only addressing the general quarter calendar to address @Pavling. The rest of Pavling's questions are perfectly valid and unclear in the original question.Kliber
I removed my answer below because after the original question being changed, my answer no longer was correct...Kliber
G
5

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.

Gauss answered 7/12, 2011 at 11:47 Comment(15)
so this will return an array of three months...does this mean the caller then has to identify which quarter has those three months?Kliber
@jaydel : yes exaclty or else he can modify the function!! Stackoverflow is to help and not to code for poepleGauss
@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 !!Gauss
@jaydel : Please dont atleast let somobody down unnecessarily when you cant appreciate their efforts!!Gauss
@jaydel : His answer meets the questionaire's requirement completely!!Gauss
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...Kliber
I removed my answer because it answered the original question not the changed version and as such is just signal to noise.Kliber
@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 :)Gauss
@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 :PKliber
@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.Kliber
@jaydel : Yes but the questionare wudnt have accepted the answer if it didnt meet up his requirement ryt bro !!Gauss
If nothing else, the OP may have learnt a lesson in asking smart questions ;-) catb.org/~esr/faqs/smart-questions.htmlGroce
@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 :PKliber
@jaydel : I personally feel your answer was the most accurate one if all he wanted was just the qaurter!!!Gauss
@Mithun: Thanks :) Enjoyed our exchange here. Good stuffKliber
G
31

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
Grate answered 26/9, 2013 at 14:34 Comment(1)
Such a clean and perfect answer. Not sure why this hasn't been accepted.Demount
B
6

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"
Beloved answered 16/5, 2013 at 13:32 Comment(1)
This requires ActiveSupportHistoriography
G
5

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.

Gauss answered 7/12, 2011 at 11:47 Comment(15)
so this will return an array of three months...does this mean the caller then has to identify which quarter has those three months?Kliber
@jaydel : yes exaclty or else he can modify the function!! Stackoverflow is to help and not to code for poepleGauss
@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 !!Gauss
@jaydel : Please dont atleast let somobody down unnecessarily when you cant appreciate their efforts!!Gauss
@jaydel : His answer meets the questionaire's requirement completely!!Gauss
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...Kliber
I removed my answer because it answered the original question not the changed version and as such is just signal to noise.Kliber
@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 :)Gauss
@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 :PKliber
@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.Kliber
@jaydel : Yes but the questionare wudnt have accepted the answer if it didnt meet up his requirement ryt bro !!Gauss
If nothing else, the OP may have learnt a lesson in asking smart questions ;-) catb.org/~esr/faqs/smart-questions.htmlGroce
@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 :PKliber
@jaydel : I personally feel your answer was the most accurate one if all he wanted was just the qaurter!!!Gauss
@Mithun: Thanks :) Enjoyed our exchange here. Good stuffKliber
I
2

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
Inglebert answered 30/3, 2015 at 18:46 Comment(0)
D
2

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
Debility answered 16/2, 2016 at 4:38 Comment(2)
This works, but in Example Usage d.quarter should return 2 not 4Prizewinner
Monkey patching Date with quarter is a really nice way to go when you're dealing with quarters all the time. Just make sure to leave copious notes so everybody knows it's there. :)Cobbett
W
0

Date#quarter (Rails 7.1+)

Starting from Rails 7.1, there is a quarter method.

Date.new(2010, 1, 31).quarter  # => 1
Date.new(2010, 4, 12).quarter  # => 2
Date.new(2010, 9, 15).quarter  # => 3
Date.new(2010, 12, 25).quarter # => 4

So now, to get the quarter is as simple as this:

Date.parse("27-04-2011").quarter
# => 2

Sources:

Notes:

Wisecrack answered 3/1 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.