2

私は今何時間も探していましたが、正しい答えを見つけることができません。
あなたが私を助けてくれることを願っています。

Ruby: 1.9.3p194
Rails: 3.2.8

ゴール:

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]

get_weeknumbers_in_monthしたがって、メソッドが呼び出されたときに、実際にはカレンダーの週番号の配列が必要です。


リサーチ:

日付から月番号を取得するには:

DateTime.parse("2012-01-10").month
=> 1



1か月の日数を取得するには:

Time::days_in_month(1, 2012)
=> 31



日付の暦週番号を取得するには:

DateTime.parse("2012-04-12").strftime("%W").to_i
=> 15



誰かがこの問題について私を助けてくれますか?

前もって感謝します。

4

1 に答える 1

0

私が理解すれば、あなたはほとんど終わった!最後は月の最初と最後の日付を計算することですよね?したがって、nextのようなものが機能する可能性があります(テストされていません)

def get_weeknumbers_in_month(month, year)
  first_week_num = Date.new(year,month,1).strftime("%U").to_i
  last_week_num = Date.new(year,month,1).end_of_month.strftime("%U").to_i
  (first_week_num..last_week_num).to_a
end
于 2012-11-06T15:30:46.863 に答える