Ruby On Rails, week number is not the correct one (-1)

浪子不回头ぞ 提交于 2019-12-07 06:18:14

问题


I need the current week number, if I'm not totally mistaken it's week 51 right now? However when testing it in the console I get this.

Time.now
=> 2013-12-19 11:08:25 +0100
Time.now.strftime('%U')
=> "50"

Date.today
=> Thu, 19 Dec 2013
Date.today.strftime("%U").to_i
=> 50

Why is that?


回答1:


Time.now.strftime('%V') will give you the week number according to ISO 8601.




回答2:


why is that?

according to %U or %W, The days in the year before the first week are in week 0 (00..53).

with %V (as @Graeme McLean wrote), The days in the year before the first week are in the last week of the previous year (01..53).

From here.




回答3:


Hmm, I'm unsure as to why it is that way, but to get the correct one using Ruby, I use this:

require 'Date'

week_number = Date.today.cweek #=> 51


来源:https://stackoverflow.com/questions/20678784/ruby-on-rails-week-number-is-not-the-correct-one-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!