Get last day of the month in Ruby

后端 未结 6 1569
-上瘾入骨i
-上瘾入骨i 2020-12-12 20:32

I made new object Date.new with args (year, month). After create ruby added 01 number of day to this object by default. Is there any way to add not first day, but last day o

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 21:13

    To get the end of the month you can also use ActiveSupport's helper end_of_month.

    # Require extensions explicitly if you are not in a Rails environment
    require 'active_support/core_ext' 
    
    p Time.now.utc.end_of_month # => 2013-01-31 23:59:59 UTC
    p Date.today.end_of_month   # => Thu, 31 Jan 2013
    

    You can find out more on end_of_month in the Rails API Docs.

提交回复
热议问题