我有一个 UTC 时间戳
I have a timestamp that is in UTC
2010-10-25 23:48:46 UTC"
"2010-10-25 23:48:46 UTC"
我需要将其转换为 ISO 8601
I need to convert it into ISO 8601
2010-10-29 06:09Z"
"2010-10-29 06:09Z"
文档令人困惑 - 最简单的方法是什么?
The documentation is confusing as hell - what is the easiest way to do that?
推荐答案我认为你在欺骗我们.
您问题的输入日期是 2010 年 10 月 25 日,而输出日期是 2010 年 10 月 29 日.打得好!
The input date to your question is the 25th of October, 2010, whilst the output is the 29th of October, 2010. Well played!
继续这个挑剔的线程:你的时间也完全不同,你错过了输出时间的秒数.
Continuing on this nit-picking thread: your times are also completely different and you're missing the seconds from the output time.
现在来看看真正的答案.
Now for the true answer.
首先要说明一点:Ruby 中的 ISO 8601 输出类似于 ISO 8601 的维基百科页面.
A little factoid first though: the ISO 8601 output in Ruby is similar to the "Combined date and time" output from ISO 8601's Wikipedia page.
你有一个字符串,所以你需要将它转换成一个 Time 对象,你可以用 to_time 来做.然后只需对该对象调用 iso8601 即可获取 ISO 8601 版本:
You've got a string and so you'll need to convert it into a Time object which you can do with to_time. Then it's simply a matter of calling iso8601 on that object to get the ISO 8601 version:
"2010-10-25 23:48:46 UTC".to_time.iso8601to_time 方法由 Rails 提供,而 iso8601 方法由 Ruby 的标准库提供.
The to_time method is courtesy of Rails, whilst the iso8601 is courtesy of Ruby's standard library.
更多推荐
在 Ruby 中将 UTC 时间戳转换为 ISO 8601
发布评论