如何找到一周或一个月到期的日期。
days_for_week should return 19,20,21 (assuming current date is 21st)
days_for_month should return 1..21 (assuming current date is 21st)
答案 0 :(得分:1)
对于第一个,您可以使用Time.now.wday
来获取当前的工作日,然后使用减号来获取本周开始的日期。
对于第二个,它更简单,每个月都以1st
开头,对吗?
答案 1 :(得分:1)
假设我正确地阅读了你的问题......
第二个很简单:
def days_for_month
1..Date.today.day
end
第一个需要一些算法才能回到星期六:
def days_for_week
days = []
day = Date.today
until day.saturday?
days.unshift(day.day)
day -= 1
end
days
end
答案 2 :(得分:0)
有效支持提供了许多有用的方法,例如at_beginning_of_week,at_end_of_week,at_beginning_of_month等。
> Date.today.at_beginning_of_week
=> Mon, 20 May 2013
对于这种特殊情况,你可以做
> (Date.today.at_beginning_of_week..Date.today).map &:day
=> [20, 21]
类似地
> (Date.today.at_beginning_of_month..Date.today).map &:day
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
或只是
> 1..Date.today.day