How to Get the Current Unix Time in Ruby
Unix time counts the seconds since 1 January 1970 UTC, and many formats store times that way. In Ruby, Time.now.to_i gives the current Unix time as a whole number, and Time.at turns such a number back into a Time.
Syntax
<seconds> = Time.now.to_i
<time> = Time.at(<seconds>).utc
| Part | What it does |
|---|---|
Time.now | The current time. |
.to_i | Its Unix time in whole seconds; fractions are dropped. |
Time.at(<seconds>) | A Time for those seconds. |
.utc | Shows that time in UTC. |
The Ruby documentation for Time#to_i: "Returns the value of self as integer Epoch seconds; subseconds are truncated (not rounded)".
The Example
The example prints the current Unix time, then the same moment as a UTC date and time.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

now = Time.now.to_i
puts now
puts Time.at(now).utc
Step 2: Run It
ruby main.rb
1795679779
2026-11-26 07:56:19 UTC

Your numbers will differ. The command is the same in zsh, bash and PowerShell.
Times in the Future
Unix time is a plain number of seconds, so adding time is arithmetic: Time.now.to_i + 15 * 60 is 15 minutes from now.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Printing Time.at(...) without .utc | It's shown in the computer's local time zone | Add .utc |
Using to_f where whole seconds are expected | You get a decimal with the fraction of a second | Use to_i |
| Treating the number as milliseconds | It's seconds; JavaScript's Date.now() is milliseconds | Multiply or divide by 1,000 when exchanging values |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A converted time is hours off | It was shown in local time | Call .utc |
| A date far in the future | Milliseconds were read as seconds | Divide by 1,000 |
FAQ
How do I get fractions of a second?
Time.now.to_f returns the Unix time as a Float.
Does the time zone change the number?
No. Unix time counts seconds since a fixed UTC moment; the zone only matters when you display it.
Conclusion
Time.now.to_i returns the current Unix time in seconds, arithmetic moves it, and Time.at(seconds).utc turns it back into a readable UTC time.
Additional Resources
- Time, in the Ruby documentation.
