如何在Ruby中找到即将到来的周末的日期?

时间:2011-09-22 21:37:46

标签: ruby date

所以我希望有一个名为Weekend Delivery的选项,但我似乎找不到任何有用的Date#方法来帮助确定星期六的日期(比如2011-09-24),比如说。

到目前为止,我提出了:

today = Date.today
sat = today + (6-today.wday) # 6 being the 6th day of the week with 1 being Monday

不是说它不够短,但我想如果有任何内置或Gem方法可以执行Date.this_sat,Date.this_thur,Date.this_weekend之类的操作,那就太好了..

谢谢!

2 个答案:

答案 0 :(得分:3)

使用Chronic gem,太棒了!

gem install chronic

以下是一个例子:

require 'chronic'
Chronic.parse('this Saturday noon')
#=> Sat Sep 24 12:00:00 PDT 2011

我编辑了输出以反映最新的gem版本,因为我使用的是旧版本。

答案 1 :(得分:3)

好吧,ActiveSupport有一个end_of_week方法。减1并且你在那里:周末!