如何在 Rails 3 中将 Unix 时间戳更改为人类日期
我看过几篇文章对 mysql 进行了相反的操作,但我正在寻找一种方法将 unix 时间戳更改为人类可读的日期(理想情况下我可以更改其格式),但我一直无法到目前为止找到任何东西。
我正在存储从 XML feed 中提取的日期,通过 NokoGiri(在 Rails 3.1.1 中)作为散列的一部分:
'date' => i.xpath('@unix-timestamp')
这可以很好地获取数字,但是你到底如何将这个 DD-MM-YYYY 放入其中我的观点之一?
我尝试过 Time.at( (i.xpath('@unix-timestamp') ) 没有效果;我只是收到错误“无法将 Nokogiri::XML::NodeSet 转换为精确数字”,现在我碰壁了
非常感谢您的帮助!
I've seen a couple of posts doing the reverse for mysql, but I'm looking for a way to change a unix timestamp to a human readable date (ideally one I can change the format of) and I haven't been able to find anything so far.
I'm storing a date pulled from an XML feed, via NokoGiri (in Rails 3.1.1) as part of a hash:
'date' => i.xpath('@unix-timestamp')
which gets the number fine, but how the devil do you make this DD-MM-YYYY to be put in one of my views?
I've tried Time.at( (i.xpath('@unix-timestamp') ) to no avail; I just get the error 'can't convert Nokogiri::XML::NodeSet into an exact number' and now I've hit a wall
Much gratitude for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Time.at
绝对是一个应该可以从纪元时间转换为 ruby Time 对象的调用(请参阅 此处 为例)。因此,您似乎需要将 XML 结果转换为更可用的内容。我想你想尝试使用 NodeSet#text 来获取字符串输出,然后将其转换为整数:在 Engineyard 博客
Time.at
is definitely a call that should work to convert from epoch time to a ruby Time object (see here for an example). So it seems like you need to work on converting your XML result into something more usable. I think you want to try using NodeSet#text to get a string output, then converting that to an integer:There's a decent but basic tutorial for NokoGiri in the Engineyard blog
您可以使用:
DateTime.strptime("1373210218",'%s')
以及
Time.at(1373210218).strftime("%B %e, %Y at %I :%M %p")
You can use like :
DateTime.strptime("1373210218",'%s')
and also
Time.at(1373210218).strftime("%B %e, %Y at %I:%M %p")