How do I add two weeks to Time.now? How do I add two weeks to Time.now? ruby ruby

How do I add two weeks to Time.now?


You don't have such nice helpers in plain Ruby. You can add seconds:

Time.now + (2*7*24*60*60)

But, fortunately, there are many date helper libraries out there (or build your own ;) )


Ruby Date class has methods to add days and months in addition to seconds in Time.An example:

require 'date't = DateTime.nowputs t      # => 2011-05-06T11:42:26+03:00# Add 14 daysputs t + 14 # => 2011-05-20T11:42:26+03:00# Add 2 monthsputs t >> 2 # => 2011-07-06T11:42:26+03:00# And if needed, make Time object out of it(t + 14).to_time   # => 2011-05-20 11:42:26 +0300


require 'rubygems'require 'active_support/core_ext/numeric/time'self.expires = 2.weeks.from_now