98

I've been a bit spoiled by the joda-time API of:

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

I'm trying to do a similar thing in Ruby, but I'm

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

However, the math is off here (I'm really working with dates at midnight).

Is there friendly API for date subtraction?

8 Answers 8

117
require 'date'
now = Date.today
ninety_days_ago = (now - 90)

Running this thru the IRB console I get:

>>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)>

If you need the time you could just say now = DateTime.now

3
  • Your original post, pre-edit was closer to what I needed, but Date.Now was protected. Date.today worked. If you change your first blurb to have the line now = Date.today, I'll accept it. Mar 2, 2011 at 18:22
  • 3
    @Matt: and how to subtract 90 hours instead of days? Feb 10, 2012 at 22:59
  • @EstebanFeldman Date also allows fractions so you can do Date.today - (90.0/24.0). The result can easily be converted to_datetime or to_time too.
    – jchook
    Jan 1, 2013 at 22:26
21

For those using Rails, check out 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
0
10

If you're using Rails or don't mind including ActiveSupport, you can use the Numeric#days DSL like this:

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 are working with dates instead of times, you should also either start with Date instances, or convert your DateTime intances with #to_date. When adding/subtracting numbers from date instances, 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)> 
10

Ruby supports date arithmetic in the Date and DateTime classes, which are part of Ruby's standard library. Both those classes expose #+ and #- methods, which add and subtract days from a date or a 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 operate on months instead of days. Arithmetic on months is a little different than arithmetic on days. 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 your joda-time example, you might write something like this in Ruby.

now =  DateTime.now
ninety_days_ago = now - 90

or maybe just

ninety_days_ago = DateTime.now - 90
9

use the number of seconds:

Time.now - 90*24*60*60
5

This is a super old post, but if you wanted to keep with a Time object, like was originally asked, rather than switching to a Date object you might want to consider using Ruby Facets.

Ruby Facets is a standardized library of extensions for core Ruby classes.

http://rubyworks.github.io/facets/

By requiring Facets you can then do the following with Time objects.

Time.now.less(90, :days)

2
  • This project hasn't been updated for two years and has opened issues with a single comment. It's dead. Jul 7, 2018 at 23:41
  • 1
    @user4674453 The last update was six months ago. It's a stable library that still works with the latest version of Ruby. Not all codebases need to be updated daily in order to not be considered "dead".
    – bigtunacan
    Jul 12, 2018 at 18:15
0

Gem install chronic

require 'chronic'
$wdate = Chronic.parse '7 days ago'
-2

Simple solution using Rails Active Support:

days90_ago = 90.days.ago.to_date.to_s

OUTPUT:
puts 90_days_ago
=> "2019-10-09" # considering cur_date: 2020-01-07
1
  • 1
    This is using ActiveSupport (Rails).
    – Espen
    Jul 31, 2020 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.