Ruby: Format already formatted date string
I have a date string that comes back like this: 02-22-2011
I need to turn that into Feb 22, 2011... is there anyw开发者_StackOverflow中文版ay to easily due this in Ruby?
Figured it out! I used Chronic (http://chronic.rubyforge.org/)
Chronic.parse('02-22-2011').strftime("%b %d, %Y")
Instead of requiring a gem just for this, you can also use Time.parse
.
I would recommend Date.strptime. It's kind of a revert strftime. Then you can use strftime to get the string version of the date.
require 'date'
p Date.strptime('02-22-2011', '%m-%d-%Y') #-> #<Date: 2011-02-22 (4911229/2,0,2299161)>
p Date.strptime('02-22-2011', '%m-%d-%Y').strftime("%b %d, %Y") #"Feb 22, 2011"
精彩评论