Rails 3.2.8 - How do I get the week number from Rails?

后端 未结 4 906
难免孤独
难免孤独 2021-01-01 11:21

I would like to know how to get the current week number from Rails and how do I manipulate it:

  1. Translate the week number into date.
  2. Make an interval
4条回答
  •  粉色の甜心
    2021-01-01 11:45

    Date#cweek seems to get the ISO-8601 week number (a Monday-based week) like %V in strftime (mentioned by @Robban in a comment).

    For example, the Monday and the Sunday of the week I'm writing this:

    [ Date.new(2015, 7, 13), Date.new(2015, 7, 19) ].map { |date|
      date.strftime("U: %U - W: %W - V: %V - cweek: #{date.cweek}")
    }
    
    # => ["U: 28 - W: 28 - V: 29 - cweek: 29", "U: 29 - W: 28 - V: 29 - cweek: 29"]
    

提交回复
热议问题