I’ve been searching for hours now and I just can’t find the right answer.
Hope you can help me out.
Ruby: 1.9.3p194
Rails: 3.2.8
Goal:
1.9.3p194 :001 > get_weeknumbers_in_month 1, 2012
=> [1, 2, 3, 4]
1.9.3p194 :002 > get_weeknumbers_in_month 2, 2012
=> [5, 6, 7, 8]
1.9.3p194 :003 > get_weeknumbers_in_month 3, 2012
=> [9, 10, 11, 12, 13]
1.9.3p194 :004 > get_weeknumbers_in_month 4, 2012
=> [14, 15, 16, 17]
So, I actually want an array of calendar week numbers when the method get_weeknumbers_in_month is called.
Research:
To get the month numbers from a date:
DateTime.parse("2012-01-10").month
=> 1
To get the number of days in a month:
Time::days_in_month(1, 2012)
=> 31
To get the calendar week number of a date:
DateTime.parse("2012-04-12").strftime("%W").to_i
=> 15
Can someone please help me with this issue?
Thanks in advance.
If I understand, you’ve almost done!
the last is to calculate first and last dates for month, isn’t it?
So something like next may work (not tested)