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

1 回答 1

1

a_date.wday星期几,你可以通过删除它找到星期天。从那里,只需在数组中添加周日和接下来的几天。上周,只是该数组的每个元素减去 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
于 2013-04-01T09:13:36.353 回答