Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 825
Mithun Sasidharan Avatar asked Dec 07 '11 11:12

Mithun Sasidharan


1 Answers

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
like image 183
Tintin81 Avatar answered Sep 19 '22 16:09

Tintin81