Ruby 周数结果

Ruby week number results

为什么我用

Time.now.strftime('%Y%W')

Date.today.strftime('%Y%W')

他们 return 201912 而它应该是 201913 因为我们在第 13 周,而不是第 12 周。

如何获取当前周数?

Time.now.strftime('%Y%V')

会给你想要的输出。

%V - Week number of the week-based year (01..53)

你必须寻找基于周的年份。

如果您查看文档,它是这样说的:

Week number: The first week of YYYY that 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)

2019 年 1 月 1 日是星期二,所以那应该是第 0 周 - 今天是第 12 周。