Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a '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_date on it, I am getting this error:

undefined method `to_date' for 1296524384:Fixnum
like image 654
marcamillion Avatar asked Dec 05 '11 02:12

marcamillion


People also ask

How do I change the date format in Ruby on Rails?

You need to convert your string into Date object. For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.


1 Answers

You can just do:

the_time = Time.at(1296524384)

That gives you:

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

By the way, 1296524384 is referred to as UNIX or epoch time. It measures the number of seconds elapsed since January 1, 1970.

To format it a bit better, you can use Ruby's strftime method.

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

More info here: http://apidock.com/ruby/DateTime/strftime

like image 167
iwasrobbed Avatar answered Sep 28 '22 03:09

iwasrobbed