Get last day of the month with Ruby
last_day_of_the_month = (Date.parse(year_to_process + "-" + (month_to_process.to_i + 1).to_s + "-01") - 1).mday().to_s rescue "31"
The final rescue is due to catch the error when month_to_process is 12.
last_day_of_the_month = (Date.parse(year_to_process + "-" + (month_to_process.to_i + 1).to_s + "-01") - 1).mday().to_s rescue "31"
The final rescue is due to catch the error when month_to_process is 12.
August 31st, 2009 at 10:57 pm
Won't (month + 1) returns 13 when december!!!!!????????
September 1st, 2009 at 9:28 am
You are right. Just changed the function to works for December too.
January 19th, 2010 at 3:11 pm
Or do this…
Date.parse( "#{year}-#{ (month + 1) % 12 }-01" ) - 1
February 9th, 2010 at 6:22 pm
Martin: does not work if month = 11…
(11+1)%12 = 0…