Ruby:将 Unix 时间戳转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10331926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:04:11  来源:igfitidea点击:

Ruby: convert unix timestamp to date

rubydatetimeunix-timestamp

提问by Paul

irb> dt = Time.now.utc
=> 2012-04-26 10:47:01 UTC

irb> dti = dt.to_i
=> 1335437221

Now, how to convert dtiback to Date/Time ?

现在,如何转换dti回 Date/Time ?

回答by Simone Carletti

Use Time.at.

使用Time.at.

Time.at(1335437221)
# => 2012-04-26 12:47:01 +0200