How do you convert YYYY-MM-DDTHH:MM:SSZ time format to MM/DD/YYYY time format in Ruby?
For example, I'm trying to convert 2011-01-19T00:00:00Z to 01/19/2011. What's the simplest way of accomplish开发者_JS百科ing this?
You can use Time.parse
to parse a time string like that into a Time
object. You can then use strftime
to turn this into any string format you like:
require 'time'
Time.parse("2011-01-19T00:00:00Z").strftime("%m/%d/%Y")
#=> "01/19/2011"
You can use the Ruby Time object to parse and then re-format time and date:
require 'time' time = Time.strptime("2011-01-19T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ") time.strftime("%m/%d/%Y") => 01/19/2011
If you want to take the time-zone into consideration use this line instead:
time = Time.strptime("2011-01-19T00:00:00Z", "%Y-%m-%dT%H:%M:%S%z")
精彩评论