Rails:有没有得到最近的星期一到今天的Date对象? [英] Rails: Is there away to get the Date object that is the closest Monday to today?

查看:102
本文介绍了Rails:有没有得到最近的星期一到今天的Date对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道我可以做一些事情:



Date.tomorrow
Date.today



有没有像Date.nearest这样的东西:星期一?

解决方案

commercial 方法在Date对象上会让你这样做。

  Date.commercial(Date.today.year,1 + Date.today.cweek ,1)

如果您需要下一个或上一个星期一,以最接近的人为准,您可以执行以下操作: / p>

  Date.commercial(Date.today.year,Date.today.cwday.modulo(4)+ Date.today.cweek, 1)

我现在无法执行,所以原谅我,如果有语法错误。 / p>

Given a date, how do I find the nearest Monday in Rails?

I know I can do things like:

Date.tomorrow Date.today

Is there something like Date.nearest :monday ?

解决方案

The commercial method on the Date object will let you do this. This example will get you the next Monday.

Date.commercial(Date.today.year, 1+Date.today.cweek, 1)

If you need the next or previous Monday, whichever is closest, you can do:

Date.commercial(Date.today.year, Date.today.cwday.modulo(4)+Date.today.cweek, 1)

I can't execute this right now, so forgive me if there are syntax errors.

这篇关于Rails:有没有得到最近的星期一到今天的Date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆