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 → -1, 0, +1 or nil

Compares the two dates and returns -1, zero, 1 or nil. The other should be a date object or a numeric value as an astronomical Julian day number.

Example
Date.new(2001,2,3) <=> Date.new(2001,2,4)   #=> -1
Date.new(2001,2,3) <=> Date.new(2001,2,3)   #=> 0
Date.new(2001,2,3) <=> Date.new(2001,2,2)   #=> 1
Date.new(2001,2,3) <=> Object.new           #=> nil
Date.new(2001,2,3) <=> Rational(4903887,2)  #=> 0

See also Comparable.

See also #<< #=== #- #>> also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1