DevToolsForYou

Unix Timestamp Converter in Ruby — Code Examples

Unix Timestamp Converter in RubyUse the online tool →

A Unix timestamp is the number of seconds (or milliseconds) since 1 January 1970 00:00:00 UTC. Here is how to get the current timestamp, convert to a human-readable date, and parse date strings back to timestamps in each language.

Ruby's Time class works natively with Unix timestamps. Use Time.now.to_i for the current epoch and Time.at to convert back.

Ruby
require 'time'

# Current Unix timestamp (seconds)
now = Time.now.to_i
puts now

# Current timestamp in milliseconds
now_ms = (Time.now.to_f * 1000).to_i
puts now_ms

# Timestamp → Time object
t = Time.at(1700000000)
puts t.utc.strftime("%Y-%m-%d %H:%M:%S UTC") # 2023-11-14 22:13:20 UTC

# Timestamp → formatted string with timezone
t_ist = Time.at(1700000000).getlocal("+05:30")
puts t_ist.strftime("%Y-%m-%d %H:%M:%S %Z") # 2023-11-15 03:43:20 +0530

# ISO 8601 string → timestamp
ts = Time.parse("2023-11-14T22:13:20Z").to_i
puts ts # 1700000000

# DateTime string → timestamp (any format)
ts2 = Time.strptime("14/11/2023 22:13:20", "%d/%m/%Y %H:%M:%S").to_i
puts ts2
Notes & gotchas
  • Time.now.to_i returns whole seconds; use Time.now.to_f for fractional seconds or * 1000 for milliseconds.
  • Time.at always returns a Time object in the local timezone — call .utc to switch to UTC.
  • require 'time' is needed for Time.parse; it is not loaded by default.
Try it in your browser

Need to epoch/unix converter without writing code? The Unix Timestamp Converter runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Epoch/Unix Converter
Unix Timestamp Converter in other languages