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

Time#to_s

time.to_s → string

Returns a string representing time. Equivalent to calling strftime with the appropriate format string.

Example
t = Time.now
t.to_s                              #=> "2012-11-10 18:16:12 +0100"
t.strftime "%Y-%m-%d %H:%M:%S %z"   #=> "2012-11-10 18:16:12 +0100"

t.utc.to_s                          #=> "2012-11-10 17:16:12 UTC"
t.strftime "%Y-%m-%d %H:%M:%S UTC"  #=> "2012-11-10 17:16:12 UTC"
See also #to_r #to_time #to_json #tuesday? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1