如何查找 Ruby 即将到来的周末的日期?
所以我想要一个名为“周末交付”的选项,但我似乎找不到任何漂亮的 Date#method 可以帮助确定星期六的日期(例如 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 之类的操作,那就太好了..
谢谢!
so I wanted to have an option called Weekend Delivery, but I can't seem to find any nifty Date#method that can help determine the date (like 2011-09-24) for Saturday, say.
So far I come up with:
today = Date.today
sat = today + (6-today.wday) # 6 being the 6th day of the week with 1 being Monday
Not that it's not short enough, but I thought if there are any built-in or Gem method that does something like Date.this_sat, Date.this_thur, Date.this_weekend, it would be nice..
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用慢性宝石,太棒了!
这是一个示例:
我编辑了输出以反映最新的 gem 版本,因为我使用的是旧版本。
Use the Chronic gem, it is fantastic!
Here is an example:
I edited the output to reflect the newest gem version since I am using an older version.
嗯,ActiveSupport 有一个 end_of_week 方法。减 1 就到了:周末!
Well, ActiveSupport has an end_of_week method. Subtract 1 and you're there: weekend!