NothingToSeeHere
NothingToSeeHere

Reputation: 2363

Rails 4 3 years from now on a given date

I'm trying to find a way to figure out "3 years from now on x date" where x date is not today.

def xmas
   self.new_xmas = @given_date + 3.years 
end

I'm stuck on how to make sure it's December 25th 3 years from @given_date

Upvotes: 1

Views: 686

Answers (2)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34318

You can simply do this:

require 'date'

given_date = Date.today # you can change it to any date
puts "given_date: #{given_date}"
current_year = given_date.year # grab the current year from the given_date
third_xmas_from_given_date = Date.new(current_year + 3, 12, 25) # add 3 years to the current_year (change it according to your need)
puts "third_xmas_from_given_date: #{third_xmas_from_given_date}"
# => given_date: 2015-11-09
# => third_xmas_from_given_date: 2018-12-25

Upvotes: 1

baron816
baron816

Reputation: 701

You can do stuff like Date.new(2015,12,25).next_year(3).

Upvotes: 1

Related Questions