如何计算当前日期的过去日期

时间:2013-04-01 08:48:39

标签: ruby

  1. 将日期作为“本周”的数组返回
  2. def this_week   
      if today == "wednesday"
        return [date(sunday), ..., date(wednesday)]       
      end 
    end
    
    1. 将日期作为“上周”的数组返回
    2.  def last_week
          return [date(last.sunday), ..., date(last.saturday)]
        end
      

1 个答案:

答案 0 :(得分:1)

a_date.wday给你the day of the week,你可以通过删除它来找到星期日。从那里,只需在数组中添加星期日和接下来的几天。在过去的一周里,只是该数组的每个元素减去7天。

require 'date'
def this_week(today)
  sunday = today - today.wday
  week = [sunday]
  (1..6).each do |wday|
    week << sunday + wday
  end

  return week
end

def last_week(today)
  return this_week(today).map { |elem| elem - 7}
end