【发布时间】:2012-01-18 12:56:15
【问题描述】:
我有一个方法可以返回一个月第一周的第一天,用于制作小日历:
def month_first_week_day(month, year)
start = DateTime.civil(year, month, 1)
start_date = DateTime.commercial(year, cweek, 1)
end
但我有这个问题:
Date.civil(2011, 1, 1).cweek # => 52
我希望它返回 1,而不是 52。我在 java 中发现了同样的问题:Why does the en_GB locale think the 1st of January is the 52nd week of the year?。
有什么建议吗?
【问题讨论】:
-
1 月 1 日并不总是属于一年中的第一周。第一周定义为第一周的时间,但该年的第一周开始的时间。根据定义,第 1 次将在 7 次中有 6 次进入上一年。因为这样的话,12 月的后端周将低于第 1 周,如您所期望的那样。
-
你用什么标准说 1 月 1 日总是第 1 周?如果
Friday Jan 1st是第 1 周,那么Monday Jan 4th是哪一周?尽管中间有一个周末,但它仍然是第 1 周吗? -
@Gareth 这是一个类似日历的视图,我应该看到的第一周是有 1 月 1 日的那一周,不管是哪一天。
标签: ruby-on-rails date