This question already has an answer here:
这个问题在这里已有答案:
- How to convert a unix timestamp (seconds since epoch) to Ruby DateTime? 6 answers
如何将unix时间戳(自纪元以来的秒数)转换为Ruby DateTime? 6个答案
I'm getting this stamp from an external API:
我从外部API获取此标记:
1391655852
and I need to convert it to a Date. I have seen a lot of conversion pages on the internet and on SO, but they all seem to be going the other way. I'm not familiar with this integer format at all.
我需要将它转换为日期。我在互联网和SO上看到了很多转换页面,但它们似乎都是另一回事。我根本不熟悉这种整数格式。
WHY THIS IS NOT A DUPLICATE QUESTION *
为什么这不是一个重复的问题*
This question should not be marked as a duplicate because the referenced question specifically mentions seconds since epoch
. While that is actually the number I was looking for, I had no idea it was called that. Judging by the very quick up-votes and a favorite as well as 40 views in less than one week, I would judge that the seconds since epoch
time format is not universally known among programmers. Thus SO users will benefit from the question being asked both ways.
这个问题不应该被标记为重复,因为引用的问题特别提到了自纪元以来的秒数。虽然这实际上是我想要的数字,但我不知道它被称为那个。在不到一周的时间里,通过非常快速的投票和最喜欢的以及40个观点判断,我将判断,自纪元时间格式以来的秒数在程序员中并不普遍。因此,SO用户将从双向询问的问题中受益。
2 个解决方案
#2
6
I'll definitely yield to @duck's answer - in my opinion, that's the best way to convert an integer to Time
- but if you're explicitly looking for a Date
, you'll want to do a bit more work:
我肯定会屈服于@ duck的答案 - 在我看来,这是将整数转换为Time的最佳方法 - 但是如果你明确地寻找一个Date,你会想要做更多的工作:
require 'date'
t = Time.at(i)
date = t.to_date
In the above example, date
will be of class Date
.
在上面的示例中,日期将是Date类。
#1
#2
6
I'll definitely yield to @duck's answer - in my opinion, that's the best way to convert an integer to Time
- but if you're explicitly looking for a Date
, you'll want to do a bit more work:
我肯定会屈服于@ duck的答案 - 在我看来,这是将整数转换为Time的最佳方法 - 但是如果你明确地寻找一个Date,你会想要做更多的工作:
require 'date'
t = Time.at(i)
date = t.to_date
In the above example, date
will be of class Date
.
在上面的示例中,日期将是Date类。