Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Time#to_f

to_f → float

Returns the value of self as a Float number Epoch seconds; subseconds are included.

The stored value of self is a Rational, which means that the returned value may be approximate:

Example
Time.utc(1970, 1, 1, 0, 0, 0).to_f         # => 0.0
Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_f # => 0.999999
Time.utc(1950, 1, 1, 0, 0, 0).to_f         # => -631152000.0
Time.utc(1990, 1, 1, 0, 0, 0).to_f         # => 631152000.0

Related: Time#to_i, Time#to_r.

See also #to_datetime #to_i #to_date #to_json also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0