ruby Rails 3.2.8 - 如何从 Rails 获取周数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13075617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:29:19  来源:igfitidea点击:

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

rubyruby-on-rails-3

提问by Chim Kan

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

我想知道如何从 Rails 获取当前周数以及如何操作它:

  1. Translate the week number into date.
  2. Make an interval based on week number.
  1. 将周数转换为日期。
  2. 根据周数进行间隔。

Thanks.

谢谢。

回答by meagar

Use strftime:

使用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)

%U- 一年中的周数。一周从星期日开始。(00..53)
%W- 一年中的周数。一周从星期一开始。(00..53)

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

# Or...

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

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

如果你想添加43周(或天数,年,分钟等)的日期,你可以使用43.weeks,通过提供的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

回答by Mike

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

你会想要远离strftime("%U")"%W"

Instead, use Date.cweek.

相反,使用Date.cweek.

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

问题是,如果您想获取周数并将其转换为日期,strftime则不会为您提供可以传递回Date.commercial.

Date.commercialexpects 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...)

Date.commercial期望一系列基于 1 的值。 Date.strftime("%U|%W")返回一个基于 0 的值。你会认为你可以+1它就可以了。这个问题会在有 53 周的年末出现。(就像刚刚发生的事情......)

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

例如,让我们看看 2015 年 12 月的结束日期以及您获取周数的两个选项的结果:

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:

如果您盲目地将传递给的值 +1,则Date.commercial在其他情况下最终会得到无效日期:

For example, December 2014:

例如,2014 年 12 月:

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.

如果您必须将该周数转换回日期,唯一可靠的方法是使用Date.cweek.

回答by Hitham S. AlQadheeb

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

EG:输入(周数):22

输出:06/12/08 - 06/19/08

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

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

回答by Henrik N

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

Date#cweek似乎像%Vstrftime一样获得 ISO-8601 周数(基于星期一的周)(@Robban 在评论中提到)。

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"]