Rails: Comparison of Dates - before? and after?

Posted . Visible to the public.

tl;dr

Since Rails 6+ you can use before? and after? to check if a date/time is before or after another date/time.

Example

christmas = Date.parse('24.12.2022')
date_of_buying_a_gift = Date.parse('12.12.2022')

date_of_buying_a_gift.before?(christmas)
# => true
# Now you are well prepared for Christmas!! ;)

date_of_buying_a_gift = Date.parse('26.12.2022')

date_of_buying_a_gift.after?(christmas)
# => true
# Now you are too late for christmas! :(

Hint

If you want to check if a date/time is between to other dates/times, you can use between? Show archive.org snapshot .

Info

Technically before? and after? are only aliases for :< and :>, for details see here Show archive.org snapshot .

Julian
Last edit
Julian
License
Source code in this card is licensed under the MIT License.
Posted by Julian to makandra dev (2022-09-08 11:52)