Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting Time class object to RFC3339 in Ruby

Tags:

time

ruby

iso

Google Calendar API(v2)'s time-related query is required to be RFC3339-formatted. When I looked up Time class after 'require "time"', I could not see rfc3339 method.

like image 490
taro Avatar asked Nov 06 '12 08:11

taro


2 Answers

The way I chose to do this was Time.now.utc.strftime('%FT%TZ') #=> "2013-08-15T06:13:28Z" which is perfect for an HTML5 type='datetime' input field.

like image 183
Dave Sag Avatar answered Oct 23 '22 01:10

Dave Sag


If you are using ActiveRecord, you can use the to_datetime method to convert the time to a DateTime object.

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

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

like image 42
Rimian Avatar answered Oct 22 '22 23:10

Rimian