---
title: "Rails 5.2 Date#prev_occurring & Date#next_occurring"
description:
  "Rails 5.2 added Date#prev_occurring and Date#next_occurring to return
  specified next/previous occurring day of week."
canonical_url: "https://www.bigbinary.com/blog/rails-5-2-adds-date-methods-to-return-specified-next-or-previous-occurring-day-of-week"
markdown_url: "https://www.bigbinary.com/blog/rails-5-2-adds-date-methods-to-return-specified-next-or-previous-occurring-day-of-week.md"
---

# Rails 5.2 Date#prev_occurring & Date#next_occurring

Rails 5.2 added Date#prev_occurring and Date#next_occurring to return specified
next/previous occurring day of week.

- Author: Sushant Mittal
- Published: April 17, 2018
- Categories: Rails 5.2, Rails

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.**

```ruby
# 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](https://github.com/rails/rails/pull/26600)
`Date#prev_occurring` and `Date#next_occurring` to find next & previous
occurring day of the week.

```ruby
# find previous thursday
>> Date.prev_occurring(:thursday)
=> Thu, 22 Feb 2018

# find next thursday
>> Date.next_occurring(:thursday)
=> Thu, 01 Mar 2018
```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-2-adds-date-methods-to-return-specified-next-or-previous-occurring-day-of-week)
