Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Time#-

(::Time arg0) -> ::Float
(::Numeric arg0) -> ::Time
self - numeric → new_time
self - other_time → float

When numeric is given, returns a new Time object whose value is the difference of the numeric value of self and numeric:

Example
t = Time.new(2000) # => 2000-01-01 00:00:00 -0600
t - (60 * 60 * 24) # => 1999-12-31 00:00:00 -0600
t - 0.5            # => 1999-12-31 23:59:59.5 -0600

When other_time is given, returns a Float whose value is the difference of the numeric values of self and other_time in seconds:

Example
t - t # => 0.0

Related: Time#+.

See also #+ #<=> .zone_offset #asctime also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0