April 17, 2018
This blog is part of our Rails 5.2 series.
Before Rails 5.2, this is how we would write to find next or previous occurring day of the week.
Assume that current date is Tue, 27 Feb 2018.
# find previous thursday
>> Date.yesterday.beginning_of_week(:thursday)
=> Thu, 22 Feb 2018
# find next thursday
>> Date.tomorrow.end_of_week(:friday)
=> Thu, 01 Mar 2018
Rails 5.2 has introduced methods
Date#prev_occurring
and Date#next_occurring
to find next & previous
occurring day of the week.
# find previous thursday
>> Date.prev_occurring(:thursday)
=> Thu, 22 Feb 2018
# find next thursday
>> Date.next_occurring(:thursday)
=> Thu, 01 Mar 2018
If this blog was helpful, check out our full blog archive.