Rails: Is there away to get the Date object that is the closest Monday to today?

后端 未结 5 952
误落风尘
误落风尘 2021-02-07 12:54

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 :mond

5条回答
  •  梦谈多话
    2021-02-07 13:16

    I know this is an old thread but it's always nice to keep it current for future seekers.

    Let's assume today is say Friday the 19th of August. All I do to get my nearest Monday is this:

    monday = Date.today.monday
    

    Then from there you can go back a week or forward a week like this:

    last_monday = monday.last_week
    next_monday = monday.next_week
    

提交回复
热议问题