tags:

views:

34

answers:

3

How do I print out a ruby time using the date-time format:

2009-02-17T05:00-07:00

Like Time.now.to_datetime_s

+1  A: 

Have you checked out Time#strftime? You can read more about it at: http://ruby-doc.org/core/classes/Time.html#M000298.

Daniel Abrahamsson
Isn't the format I posted a standard xml time format? It seems like that should be included somewhere.
viatropos
I've never found any other option than using the strftime() method, and it has mostly been flexible enough. If you would like to use your format through some method in Time, remember that classes are open in Ruby, and you can add your formatting method to the class if you like.
Daniel Abrahamsson
A: 
Time.new.strftime(<format string>)
demas
+2  A: 

Time#iso8601 (or Time#xmlschema) produces output in this format. (You may have to require 'time' beforehand).

Greg Campbell
I just noticed that the format won't be exactly the same as the one you posted - your sample doesn't appear to include seconds, whereas this one does. If that's a problem, I'd recommend using strftime as suggested in the other answers.
Greg Campbell