- 返回日期爲「本週」
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
as 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
請不要,我有麻煩實施它 – Rpj
@Rpj什麼是您的預期輸入和輸出?知道這可以給你更好的答案。 –