如何在 Rails 3 中将 Unix 时间戳更改为人类日期

发布于 2024-12-25 06:51:50 字数 409 浏览 0 评论 0原文

我看过几篇文章对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2025-01-01 06:51:50

Time.at 绝对是一个应该可以从纪元时间转换为 ruby​​ Time 对象的调用(请参阅 此处 为例)。因此,您似乎需要将 XML 结果转换为更可用的内容。我想你想尝试使用 NodeSet#text 来获取字符串输出,然后将其转换为整数:

Time.at(i.xpath('@unix-timestamp').text.to_i)

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:

Time.at(i.xpath('@unix-timestamp').text.to_i)

There's a decent but basic tutorial for NokoGiri in the Engineyard blog

祁梦 2025-01-01 06:51:50

您可以使用: 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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文