特定の年の週数(ISO 8601)を計算する方法はRubyにありますか?現在ルックアップテーブルを使用していますが、使用を中止したいと思います。
3582 次
3 に答える
13
def num_weeks(year = Date.today.year)
Date.new(year, 12, 28).cweek # magick date!
end
long_iso_years = (2000..2400).select{|year| num_weeks(year) == 53}
ウィキペディアと同じリストを生成します
于 2011-10-31T11:30:21.907 に答える
5
require 'date'
def num_weeks(year = Date.today.year)
# all years starting with Thursday, and leap years starting with Wednesday have 53 weeks
# http://en.wikipedia.org/wiki/ISO_week_date#Last_week
d = Date.new(year, 1, 1)
return 53 if d.wday == 4
return 53 if d.leap? and d.wday == 3
52
end
于 2011-10-31T11:00:22.237 に答える
0
次のことができます。
require 'date'
@year = 2001 #year you want to count the number of weeks
d = Date.new @year, 12, 30 # as in Date.new
d.cweek # returns the commercial week number for the last week of the year, in this case, 52
それがあなたが探しているものなら:)
PS:それは商業年にしか機能しないので、2001年の12月31日は実際には商業週1でした
于 2011-10-31T10:24:23.080 に答える