End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Date#-
d - other → date or rational
ext/date/date_core.c
View on GitHub ↗
static VALUE
d_lite_minus(VALUE self, VALUE other)
{
if (k_date_p(other))
return minus_dd(self, other);
switch (TYPE(other)) {
case T_FIXNUM:
return d_lite_plus(self, LONG2NUM(-FIX2LONG(other)));
case T_FLOAT:
return d_lite_plus(self, DBL2NUM(-RFLOAT_VALUE(other)));
default:
expect_numeric(other);
/* fall through */
case T_BIGNUM:
case T_RATIONAL:
return d_lite_plus(self, f_negate(other));
}
}
Returns the difference between the two dates if the other is a date object. If the other is a numeric value, returns a date object pointing other days before self. If the other is a fractional number, assumes its precision is at most nanosecond.
Example
Date.new(2001,2,3) - 1 #=> #<Date: 2001-02-02 ...> DateTime.new(2001,2,3) - Rational(1,2) #=> #<DateTime: 2001-02-02T12:00:00+00:00 ...> Date.new(2001,2,3) - Date.new(2001) #=> (33/1) DateTime.new(2001,2,3) - DateTime.new(2001,2,2,12) #=> (1/2)