- 将日期作为“本周”的数组返回
def this_week if today == "wednesday" return [date(sunday), ..., date(wednesday)] end end
- 将日期作为“上周”的数组返回
def last_week return [date(last.sunday), ..., date(last.saturday)] end
def this_week if today == "wednesday" return [date(sunday), ..., date(wednesday)] end end
def last_week return [date(last.sunday), ..., date(last.saturday)] end
给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