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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With