Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails number of weeks in month

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

Thanks

like image 862
andkjaer Avatar asked Aug 02 '11 11:08

andkjaer


2 Answers

i dont know exactly what you want... But maybe you want something like this:

(Time::days_in_month(05,2010).to_f / 7)

#> 4.42857142857143

like image 109
Michael Koper Avatar answered Nov 01 '22 05:11

Michael Koper


I needed to know how many weeks including partial weeks there were in a month. Think of it like rows in a calendar. How many rows do you need? You have to consider the number of days and also what day the month starts on. October 2011 actually has 6 unique weeks for example.

This is my answer (@date is the current date):

@week_count = (0.5 + (@date.at_end_of_month.day + @date.at_beginning_of_month.wday).to_f / 7.0).round
like image 39
Bob Benedict Avatar answered Nov 01 '22 05:11

Bob Benedict