Subtracting a Ruby date (e.g. 90 days ago)

I was a bit spoiled by the joda-time API:

DateTime now = new DateTime(); DateTime ninetyDaysAgo = now.minusDays(90); 

I try to do similar in Ruby, but I

 now = Time.now ninetyDaysAgo = now - (90*24) 

However, math is disabled (I really work with dates at midnight).

Is there a friendly date subtraction API?

+66
date ruby
Mar 02 '11 at 17:25
source share
6 answers
 require 'date' now = Date.today ninety_days_ago = (now - 90) 

By running this through the IRB console, I get: <Sub>

 >>require 'date' now = Date.today ninety_days_ago = (now - 90) require 'date' => false now = Date.today => #<Date: 2011-03-02 (4911245/2,0,2299161)> ninety_days_ago = (now - 90) => #<Date: 2010-12-02 (4911065/2,0,2299161)> 

sub> If you need time, you can just say now = DateTime.now

+77
Mar 02 '11 at 17:28
source share
— -

If you use Rails or don't mind including ActiveSupport, you can use numeric # days DSL as follows:

 ruby-1.9.2-p136 :002 > Date.today => Wed, 02 Mar 2011 ruby-1.9.2-p136 :003 > Date.today - 90.days => Thu, 02 Dec 2010 

Since you work with dates, not just once, you should also start with Date instances or convert your DateTime dates to C # to_date. When adding / subtracting numbers from instances of the date, the numbers are implicitly days.

 ruby-1.9.2-p136 :016 > DateTime.now.to_date => #<Date: 2011-03-02 (4911245/2,0,2299161)> ruby-1.9.2-p136 :017 > DateTime.now.to_date - 90 => #<Date: 2010-12-02 (4911065/2,0,2299161)> 
+9
Mar 02 '11 at 18:07
source share

For those using Rails, check the following:

 DateTime.now - 10.days => Sat, 04 May 2013 12:12:07 +0300 20.days.ago - 10.days => Sun, 14 Apr 2013 09:12:13 UTC +00:00 
+9
May 14 '13 at 9:13
source share

Ruby supports date arithmetic in the Date and DateTime classes , which are part of the Ruby standard library. Both of these classes expose # + and # - methods that add and subtract days from a date or time.

 $ irb > require 'date' => true > (DateTime.new(2015,4,1) - 90).to_s # Apr 1, 2015 - 90 days => "2015-01-01T00:00:00+00:00" > (DateTime.new(2015,4,1) - 1).to_s # Apr 1, 2015 - 1 day => "2015-03-31T00:00:00+00:00" 

Use the # << and # "> methods to work in months instead of days. Monthly arithmetic is slightly different than dayly arithmetic. Using Date instead of DateTime makes the effect more obvious.

  > (Date.new(2015, 5, 31) << 3).to_s # May 31 - 3 months; 92 days diff => "2015-02-28" 

Following the joda time example, you can write something like this in Ruby.

 now = DateTime.now ninety_days_ago = now - 90 

or maybe just

 ninety_days_ago = DateTime.now - 90 
+7
Mar 21 '15 at 15:42
source share

This is a super old post, but if you want to save the Time object as originally set, instead of going to the Date object, which you might want to consider using Ruby Facets .

Ruby Facets is a standardized extension library for core Ruby classes.

http://rubyworks.imtqy.com/facets/

By querying Facets, you can do the following with Time objects.

Time.now.less(90, :days)

+5
Jul 26 '14 at 1:07
source share

use the number of seconds:

 Time.now - 90*24*60*60 
+2
Feb 08 '17 at 19:54
source share



All Articles