Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 based on week number.

Thanks.

like image 471
Chim Kan Avatar asked Oct 25 '12 19:10

Chim Kan


3 Answers

Use strftime:

%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)

Time.now.strftime("%U").to_i # 43

# Or...

Date.today.strftime("%U").to_i # 43

If you want to add 43 weeks (or days,years,minutes, etc...) to a date, you can use 43.weeks, provided by ActiveSupport:

irb(main):001:0> 43.weeks
=> 301 days

irb(main):002:0> Date.today + 43.weeks
=> Thu, 22 Aug 2013

irb(main):003:0> Date.today + 10.days
=> Sun, 04 Nov 2012

irb(main):004:0> Date.today + 1.years # or 1.year
=> Fri, 25 Oct 2013

irb(main):005:0> Date.today + 5.months
=> Mon, 25 Mar 2013
like image 94
meagar Avatar answered Oct 22 '22 10:10

meagar


You are going to want to stay away from strftime("%U") and "%W".

Instead, use Date.cweek.

The problem is, if you ever want to take a week number and convert it to a date, strftime won't give you a value that you can pass back to Date.commercial.

Date.commercial expects a range of values that are 1 based. Date.strftime("%U|%W") returns a value that is 0 based. You would think you could just +1 it and it would be fine. The problem will hit you at the end of a year when there are 53 weeks. (Like what just happened...)

For example, let's look at the end of Dec 2015 and the results from your two options for getting a week number:

Date.parse("2015-12-31").strftime("%W") = 52
Date.parse("2015-12-31").cweek = 53

Now, let's look at converting that week number to a date...

Date.commercial(2015, 52, 1) = Mon, 21 Dec 2015
Date.commercial(2015, 53, 1) = Mon, 28 Dec 2015

If you blindly just +1 the value you pass to Date.commercial, you'll end up with an invalid date in other situations:

For example, December 2014:

Date.commercial(2014, 53, 1) = ArgumentError: invalid date

If you ever have to convert that week number back to a date, the only surefire way is to use Date.cweek.

like image 20
Mike Avatar answered Oct 22 '22 09:10

Mike


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

like image 5
Sully Avatar answered Oct 22 '22 09:10

Sully