Calendar quarter calculations in Ruby and MySQL

Posted Almost 12 years ago. Visible to the public.

ActiveSupport >= 3 has

Date.parse('2011-02-10').beginning_of_quarter #=> 2011-01-01
Date.parse('2011-02-10').end_of_quarter #=> 2011-03-31

You can manually calculate the quarter index like

(Date.parse('2011-02-10').month / 3.0).ceil #=> 1

Yes, you do actually divide by 3.0, not 4.0.

MySQL has

SELECT QUARTER('2011-02-10'); #=> 1
Henning Koch
Last edit
Over 6 years ago
Henning Koch
Keywords
year, calendar
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-06-20 08:54)