How to convert "Fixnum" to a date in Rails 3.1.3?

I am trying to display this output as a date:

1296524384

But when I call .to_dateon it, I get this error:

undefined method `to_date' for 1296524384:Fixnum
+5
source share
1 answer

You can simply do:

the_time = Time.at(1296524384)

This gives you:

2011-01-31 20:39:44 -0500

By the way, it 1296524384is called UNIX or era. It measures the number of seconds elapsed since January 1, 1970.

To format it a little better, you can use the Ruby method strftime.

the_time = Time.at(1296524384).strftime("The date is %m/%d/%Y") 

Additional information here: http://apidock.com/ruby/DateTime/strftime

+16
source

All Articles