I tried doing something like
Time.now + 5.days
but that doesn't work, even though I vaguely remember seeing, and being very impressed, with being able to do something like 2.years
etc.
How do I do that in Rails 3?
This question is related to
ruby-on-rails
datetime
ruby-on-rails-3
activesupport
This definitely works and I use this wherever I need to add days to the current date:
Date.today + 5
Some other options, just for reference
-10.days.ago
# Available in Rails 4
DateTime.now.days_ago(-10)
Just list out all options I know:
[1] Time.now + 10.days
[2] 10.days.from_now
[3] -10.days.ago
[4] DateTime.now.days_ago(-10)
[5] Date.today + 10
So now, what is the difference between them if we care about the timezone:
[1, 4]
With system timezone[2, 3]
With config timezone of your Rails app[5]
Date only no time included in resultdays
, years
, etc., are part of Active Support, So this won't work in irb
, but it should work in rails console
.
Try this on Ruby. It will return a new date/time the specified number of days in the future
DateTime.now.days_since(10)
Try this on Rails
Time.new + 10.days
Try this on Ruby
require 'date'
DateTime.now.next_day(10).to_time
Source: Stackoverflow.com