Rails number of weeks in month

Viewed 4641

How can I in rails calculate the number of weeks in a given month?

Thanks

7 Answers
def number_of_weeks_in_month
  4
end
def number_of_weeks_month(start_of_month, count, end_of_month)
 if start_date > end_of_month
  return count
 else
  number_of_weeks_month(start_date.end_of_week + 1, count + 1, end_of_month)
 end
end

get number of weeks for month like this

number_of_weeks_month(Date.parse("2017-11-01"),0,Date.parse("2017-11-30"))

this return 4

Date.today.end_of_month.day/7
=> 4
Related