Unix Timestamp in Ruby

Get the current Unix timestamp

Ruby
Time.now.to_i                       # seconds: 1700000000
Time.now.to_f                       # float seconds with microseconds
(Time.now.to_f * 1000).round        # milliseconds

# Process.clock_gettime is more precise
Process.clock_gettime(Process::CLOCK_REALTIME).to_i

Convert epoch to Time

Ruby
epoch = 1700000000

Time.at(epoch)                      # local time
Time.at(epoch).utc                  # UTC
Time.at(epoch).iso8601              # "2023-11-14T22:13:20+00:00"

# From milliseconds
Time.at(epoch / 1000.0)
Time.at(0, epoch, :millisecond)     # Ruby 2.7+

# Format
Time.at(epoch).utc.strftime('%Y-%m-%d %H:%M:%S')  # "2023-11-14 22:13:20"

Convert Time to epoch

Ruby
Time.parse('2023-11-14 22:13:20 UTC').to_i    # 1700000000
Time.parse('2023-11-14T22:13:20Z').to_i

require 'time'
Time.iso8601('2023-11-14T22:13:20Z').to_i

Timezones

Ruby
require 'tzinfo'

tz = TZInfo::Timezone.get('Asia/Kolkata')
local = tz.to_local(Time.at(1700000000).utc)
puts local                           # 2023-11-15 03:43:20 +0530

Rails / ActiveSupport

Ruby on Rails
Time.current.to_i                    # current epoch (respects Time.zone)
Time.zone.at(1700000000)             # epoch → Time in app timezone
1700000000.seconds.ago               # relative time
2.hours.from_now.to_i                # future epoch

# ActiveRecord: stored as integer
user.created_at.to_i                 # epoch from datetime column

Use the live epoch converter to convert any Ruby timestamp instantly.