Ruby: convert unix timestamp to date

Paul picture Paul · Apr 26, 2012 · Viewed 37.7k times · Source
irb> dt = Time.now.utc
=> 2012-04-26 10:47:01 UTC

irb> dti = dt.to_i
=> 1335437221

Now, how to convert dti back to Date/Time ?

Answer

Simone Carletti picture Simone Carletti · Apr 26, 2012

Use Time.at.

Time.at(1335437221)
# => 2012-04-26 12:47:01 +0200