【问题标题】:Rails Active Support: Calculate last week from a specific dateRails Active Support:从特定日期计算上周
【发布时间】:2016-08-05 00:47:51
【问题描述】:

我需要计算从特定日期开始的最后一周。

我知道 Rails 有 1.week.ago.beginning_of_week1.week.ago.end_of_week 方法,但我认为这些计算只能基于当前时间。

有没有办法创建任何时间对象(不仅仅是当前)并使用类似的方法计算前一周?

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 activesupport


    【解决方案1】:

    您可以加/减Time(或Date;或DateTime):

    time = Time.parse("01/23/1995") #Valid Time object of any time in past/future
    
    week_ago = time - 1.week
    
    beginning_of_week_ago = week_ago.beginning_of_week
    end_of_week_ago = week_ago.end_of_week
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      相关资源
      最近更新 更多