Read more

Calendar quarter calculations in Ruby and MySQL

Henning Koch
June 20, 2012Software engineer at makandra GmbH

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
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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
Posted by Henning Koch to makandra dev (2012-06-20 10:54)