Using quarter method by rails 7.1

Rails have quite a few methods for use with the date and time just as strftime, beginning_of_day, mday, etc. Similarly, rails also provide some methods like beginning_of_quarter and end_of_quarter for getting dates of when the quarter for the calendar year and month is. Rails do not provide methods for getting the monthly number for the quarter. For that, we would use something like -

Before

def quarter(date)
  (date.month / 3.0).ceil
end

After

In the upcoming version of Rails 7.1, it’ll provide a method for getting quarter of the date it is used upon -

date = Date.new(2023, 6, 15)
d.quarter

Look at PR for more information.

Need help on your Ruby on Rails or React project?

Join Our Newsletter