Ruby on on rails每年都会使用strftime

时间:2014-10-14 08:49:41

标签: ruby-on-rails ruby

我有一个关于铁轨上红宝石年产量的问题:

我希望得到本周的正确周数。我位于德国,所以我的一周从星期一开始。现在我在一周 42

根据文档(APIDock)打印周数有3种不同的可能性:

文档说:

irb(main):023:0> Time.now.beginning_of_week.strftime("%Y-%U")
=> "2014-41"
irb(main):024:0> Time.now.beginning_of_week.strftime("%Y-%W")
=> "2014-41"
irb(main):025:0> Time.now.beginning_of_week.strftime("%Y-%V")
=> "2014-42"
irb(main):029:0> Time.zone
=> #<ActiveSupport::TimeZone:0x007f600a82c320 @name="Berlin", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Europe/Berlin>, @current_period=nil>

文档说:

ISO 8601 week-based year and week number:
The week 1 of YYYY starts with a Monday and includes YYYY-01-04.
The days in the year before the first week are in the last week of
the previous year.
  %G - The week-based year
  %g - The last 2 digits of the week-based year (00..99)
  %V - Week number of the week-based year (01..53)

Week number:
The week 1 of YYYY starts with a Sunday or Monday (according to %U
or %W).  The days in the year before the first week are in week 0.
  %U - Week number of the year.  The week starts with Sunday.  (00..53)
  %W - Week number of the year.  The week starts with Monday.  (00..53)

我的问题

为什么第一个(%U%W)输出错误?我真的不明白这一点!

非常感谢!

1 个答案:

答案 0 :(得分:1)

这就是区别:

  • %U - 第1周从2014 - 01 - 05(2014年第一个星期日)开始
  • %W - 第1周开始于2014 - 01 - 06(2014年第一个星期一)
  • %V - 第1周开始于2013-12-30(1月4日或之前的第一个星期一)