---
title: "Rails 6 adds before? and after? to Date and Time"
description:
  "Rails 6 adds before? and after? to Date, DateTime, Time and
  ActiveSupport::TimeWithZone classes"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-before-and-after-to-date-and-time"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-before-and-after-to-date-and-time.md"
---

# Rails 6 adds before? and after? to Date and Time

Rails 6 adds before? and after? to Date, DateTime, Time and
ActiveSupport::TimeWithZone classes

- Author: Amit Choudhary
- Published: June 26, 2019
- Categories: Rails 6, Rails

Rails 6 adds [before?](https://github.com/rails/rails/pull/32185) and
[after?](https://github.com/rails/rails/pull/32185) to
[Date](https://api.rubyonrails.org/v5.2/classes/Date.html) ,
[DateTime](https://api.rubyonrails.org/v5.2/classes/DateTime.html) ,
[Time](https://api.rubyonrails.org/v5.2/classes/Time.html) and
[ActiveSupport::TimeWithZone](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/TimeWithZone.html)
classes.

[before?](https://github.com/rails/rails/pull/32185) and
[after?](https://github.com/rails/rails/pull/32185) are aliases to
[< (less than)](https://api.rubyonrails.org/v5.2/classes/Date.html#method-i-3C-3D-3E)
and
[> (greater than)](https://api.rubyonrails.org/v5.2/classes/Date.html#method-i-3C-3D-3E)
methods respectively.

Let's checkout how it works.

#### Rails 5.2

Let's try calling `before?` on a date object in Rails 5.2.

```ruby

> > Date.new(2019, 3, 31).before?(Date.new(2019, 4, 1))

=> NoMethodError: undefined method 'before?' for Sun, 31 Mar 2019:Date
from (irb):1

> > Date.new(2019, 3, 31) < Date.new(2019, 4, 1)

=> true
```

#### Rails 6.0.0.beta2

Now, let's compare [Date](https://api.rubyonrails.org/v5.2/classes/Date.html) ,
[DateTime](https://api.rubyonrails.org/v5.2/classes/DateTime.html) ,
[Time](https://api.rubyonrails.org/v5.2/classes/Time.html) and
[ActiveSupport::TimeWithZone](https://api.rubyonrails.org/v5.2/classes/ActiveSupport/TimeWithZone.html)
objects using [before?](https://github.com/rails/rails/pull/32185) and
[after?](https://github.com/rails/rails/pull/32185) in Rails 6.

```ruby

> > Date.new(2019, 3, 31).before?(Date.new(2019, 4, 1))

=> true

> > Date.new(2019, 3, 31).after?(Date.new(2019, 4, 1))

=> false

> > DateTime.parse('2019-03-31').before?(DateTime.parse('2019-04-01'))

=> true

> > DateTime.parse('2019-03-31').after?(DateTime.parse('2019-04-01'))

=> false

> > Time.parse('2019-03-31').before?(Time.parse('2019-04-01'))

=> true

> > Time.parse('2019-03-31').after?(Time.parse('2019-04-01'))

=> false

> > ActiveSupport::TimeWithZone.new(Time.utc(2019, 3, 31, 12, 0, 0), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]).before?(ActiveSupport::TimeWithZone.new(Time.utc(2019, 4, 1, 12, 0, 0), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]))

=> true

> > ActiveSupport::TimeWithZone.new(Time.utc(2019, 3, 31, 12, 0, 0), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]).after?(ActiveSupport::TimeWithZone.new(Time.utc(2019, 4, 1, 12, 0, 0), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]))

=> false
```

Here is the relevant [pull request](https://github.com/rails/rails/pull/32185)
for adding `before?` and `after?` methods and the
[pull request](https://github.com/rails/rails/pull/32398) for moving `before?`
and `after?` to
[DateAndTime::Calculations](https://api.rubyonrails.org/v5.2/classes/DateAndTime/Calculations.html).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-before-and-after-to-date-and-time)
