Project

General

Profile

Feature #12211

introduce Date#first_of_month and Date#last_of_month

Added by araipiyo (Shunichi Arai) over 3 years ago. Updated over 3 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:74524]

Description

Proposes new methods Date#first_of_month and Date#last_of_month which can be very helpful in reporting applications which need the first and the last day of a month very frequently.

class Date
    def first_of_month
      Date.new(year, month, 1)
    end
    def last_of_month
      next_month.first_of_month.prev_day
    end
end

Current way to do it Date.new(date.year, date.month, 1) is not elegant and short enough for this simple and common operation.

History

Updated by zverok (Victor Shepelev) over 3 years ago

IMHO, consistent approach would be "allow Date/Time to be rounded to any component", there are many uses for this. Maybe something like ceil/floor/round methods set with Symbol argument? Like floor(:month) and ceil(:month)?

Updated by shyouhei (Shyouhei Urabe) over 3 years ago

Naming-wise, Rails (ActiveSupport) has Date#beginning_of_month. That name can be more familiar to other devs.

I'm against floor(:month) and such, especially against Time class. The concept of "the second that a day begins" is not that concrete when it relates to summer time. I recommend you to stick this proposal to Date only.

Updated by araipiyo (Shunichi Arai) over 3 years ago

Shyouhei Urabe wrote:

Naming-wise, Rails (ActiveSupport) has Date#beginning_of_month. That name can be more familiar to other devs.

I'm against floor(:month) and such, especially against Time class. The concept of "the second that a day begins" is not that concrete when it relates to summer time. I recommend you to stick this proposal to Date only.

I think Date#beginning_of_month is bit verbose but okay too. How about to have an alias to Date#first_of_month?

floor(:month) looks elegant, but I think it's bit counter-intuitive. How about Date#day(n), Date#wday(n), or Date#year(n) which create a new date object with diffrent day / wday / year?

Also available in: Atom PDF