Get last day of the month in Ruby

后端 未结 6 1568
-上瘾入骨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

    require "date"
    def find_last_day_of_month(_date)
     if(_date.instance_of? String)
       @end_of_the_month = Date.parse(_date.next_month.strftime("%Y-%m-01")) - 1
     else if(_date.instance_of? Date)
       @end_of_the_month = _date.next_month.strftime("%Y-%m-01") - 1
     end
     return @end_of_the_month
    end
    
    find_last_day_of_month("2018-01-01")
    

    This is another way to find

提交回复
热议问题