Today I Learned

hashrocket A Hashrocket project

Add/Subtract Months on a Date in Ruby

With ActiveSupport you can easily add months (or days, years, etc) to a date with:

pry(main)> Date.today
# => Fri, 05 Sep 2025
pry(main)> Date.today + 2.months
# => Wed, 05 Nov 2025
pry(main)> Date.today - 1.month
# => Tue, 05 Aug 2025

But what if you're working outside of Rails and without ActiveSupport? You can use the shovel (<<) operator on dates to return the date n months earlier.

pry(main)> Date.today
# => Fri, 05 Sep 2025
pry(main)> Date.today << 1
# => Tue, 05 Aug 2025

If you want to go forwards, you can use >>, or negate the value of n using <<:

pry(main)> Date.today
# => Fri, 05 Sep 2025
pry(main)> Date.today >> 2
# => Wed, 05 Nov 2025
pry(main)> Date.today << -2
# => Wed, 05 Nov 2025

Docs for << and docs for >>

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.