How do I convert a Rails DateTime object to an XML string? How do I convert a Rails DateTime object to an XML string? xml xml

How do I convert a Rails DateTime object to an XML string?


Time to XML format:

Time.now.xmlschema # implemented by Rails, not stock rubyTime.now.strftime '%Y-%m-%dT%H:%M:%S%z'

http://corelib.rubyonrails.org/classes/Time.html#M000281

To parse (Ruby 1.9 and beyond):

t = Time.now.xmlschema(str)

http://ruby-doc.org/core-1.9/classes/Time.html#M000329


Here's a pure-Ruby way:

Time.now.strftime("%Y-%m-%dT%H:%M:%S%z")

You can get more details about the various strftime options with:

ri Time.strftime