How to convert this Time integer to Date in Ruby?

I get this stamp from an external API:

1391655852 

and I need to convert it to Date. I have seen many conversion pages on the Internet and on SO, but they all seem to go the other way. I am not at all familiar with this whole format.


WHY IT IS NOT A DUPLICATE ISSUES *

This question should not be marked as duplicate, because the question mentioned specifically mentions seconds since epoch . Although this is actually the number I was looking for, I had no idea what it was called. Judging by the very fast voices and favorites, as well as 40 views in less than one week, I would judge that the time format of seconds since epoch not known among programmers. This way, SO users will benefit from a question asked in both directions.

+7
ruby ruby-on-rails ruby-on-rails-3
source share
2 answers

Use Time.at for this:

 t = Time.at(i) 
+10
source share

I will definitely succumb to @duck answer - in my opinion, the best way to convert an integer to Time - but if you are explicitly looking for Date , you'll want to do a bit more work:

 require 'date' t = Time.at(i) date = t.to_date 

In the above example, Date will have a Date class.

+6
source share

All Articles