在Ruby中将Time类对象转换为RFC3339

Google Calendar API(v2)的与时间相关的查询必须采用RFC3339格式。 当我在’require’时间”之后查找Time类时,我看不到rfc3339方法。

您需要将时间转换为DateTime对象:

 Time.now.to_datetime.rfc3339 #=> "2014-11-06T10:40:54+11:00" 

看到:
http://www.ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/Time.html

这有帮助吗? http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-rfc3339

 DateTime.parse('2001-02-03T04:05:06.123456789+07:00').rfc3339(9) #=> "2001-02-03T04:05:06.123456789+07:00" 

我选择这样做的方式是Time.now.utc.strftime('%FT%TZ') #=> "2013-08-15T06:13:28Z" ,非常适合HTML5 type='datetime'输入字段。

一个网站提到RFC3339是RSS提要中最常见的日期格式,因此转换方法实现为#xmlschema,而不是#rfc3339。

Interesting Posts