.change doesn't work for dates for an even number of months in ruby

Hi I have defined this method

def change_date
    date = Date.today    
    start_date = date.change(year: 2015, month: (2 *  3)).at_beginning_of_quarter
    p 'aaaaaa'    
    p start_date
end

      

give me the invalid date

error .change is not working or i am doing it wrong, please help me how to solve this. Thanx in advance.

+3
ruby ruby-on-rails


source to share


2 answers


This is because the month you are specifying does not have a current day.

I mean the current month (July) has 31 days, but the month you set (June) only has 30 days. You can change your code like this:



# in Rails:
date = Date.today.beginning_of_month # or Date.today.change(day: 1)

      

Then bind your "change" before the variable date

.

+2


source to share


This actually happens because today is July 31st and not all months have 31 days in it, for example June, the 6th month, has only 30 days in it.



+2


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics