Ruby code to get the date of next Monday (or any day of the week)

前端 未结 7 487
再見小時候
再見小時候 2020-12-02 14:36

Given an input of, for example,

day = \'Monday\'

how can I calculate the date of day?

def date_of_next(day)
           


        
7条回答
  •  孤街浪徒
    2020-12-02 15:14

    For anyone like me who came here looking for a solution in Rails to this problem, as of Rails 5.2 there is a much easier method to do this.

    For anyone (like the original poster) not specifically using Rails, this functionality is available in the ActiveSupport gem.

    To find the next occurring day of a week, we can simply write something like Date.today.next_occurring(:friday).

    See the documentation for more details.

提交回复
热议问题