Skip to content

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

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)
See also #+ #<< .xmlschema #<=> also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1