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

后端 未结 4 885
难免孤独
难免孤独 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:51

    date.commercial([cwyear=-4712[, cweek=1[, cwday=1[, start=Date::ITALY]]]]) → date
    Creates a date object denoting the given week date.
    
    The week and the day of week should be a negative 
    or a positive number (as a relative week/day from the end of year/week when negative). 
    They should not be zero.
    

    For the interval

    require 'date'
    
      def week_dates( week_num )
        year = Time.now.year
        week_start = Date.commercial( year, week_num, 1 )
        week_end = Date.commercial( year, week_num, 7 )
        week_start.strftime( "%m/%d/%y" ) + ' - ' + week_end.strftime("%m/%d/%y" )
      end
    
      puts week_dates(22)
    

    EG: Input (Week Number): 22

    Output: 06/12/08 - 06/19/08

    credit: Siep Korteling http://www.ruby-forum.com/topic/125140

提交回复
热议问题