Before Rails 6.0, the date/time are compared by using the equality operators like less than (<) or greater than (>), etc. Sometimes, date/time comparison using these operators takes some time to understand as these conventions are not human-readable, at least for dates.

Now, Rails 6.0 ActiveSupport introduced with two new methods before? & after? for comparing Date, Time, DateTime, and TimeWithZone (see Pull Request ).

Before Rails 6

Date.yesterday < Date.today # true
Time.current < (Time.current - 1.hour) # false
(DateTime.current - 1.day) < DateTime.current # true
DateTime.current < (DateTime.current - 1.day) # false

After Rails 6

Date.yesterday.before? Date.today # true
Time.current.after? (Time.current - 1.hour) # false
(DateTime.current - 1.day).before? DateTime.current # true
DateTime.current.after? (DateTime.current - 1.day) # false

Conclusion

The methods before? and after? are more human-readable and easy to understand than using the equality operators (’<’ and ’>’) to compare the Date, Time, DateTime, and TimeWithZone objects

Tags:RailsActiveSupport

...

Akshay Mohite

Akshay is a Ruby on Rails and ReactJS enthusiast. He likes to write blog posts. Contributes to open source whenever gets some free time.