2013-04-01 189 views
-1
  1. 返回日期爲「本週」
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 
+0

請不要,我有麻煩實施它 – Rpj

+0

@Rpj什麼是您的預期輸入和輸出?知道這可以給你更好的答案。 –

回答

1

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 
+0

我運行了這個程序,但它爲兩種方法返回相同的日期。你有交叉驗證這個嗎? http://pastie.org/7277026 – Rpj

+1

它需要一個Date對象做'Date.today'而不是'Time.now' – toch

相關問題