Calendar quarter calculations in Ruby and MySQL

Updated . Posted . 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
Profile picture of Henning Koch
Henning Koch
Last edit
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)