Unix Timestamp in Ruby
In Ruby, Time.now.to_i returns the current Unix time in seconds, and Time.at(ts) builds a Time from a timestamp. Call .utc to render it in UTC.
Get & convert epoch time in Ruby
require 'time' puts Time.at(1700000000).utc.iso8601 # 2023-11-14T22:13:20.000Z puts Time.now.to_i
Gotcha: Time.at returns local time by default — chain .utc to avoid zone-dependent output.
Frequently asked questions
- How do I get the current Unix timestamp in Ruby?
- Time.now.to_i for seconds, (Time.now.to_f * 1000).to_i for milliseconds.
- How do I convert a timestamp in Ruby?
- Time.at(ts).utc.iso8601 returns an ISO 8601 UTC string.