开发者

Ruby: Format already formatted date string

开发者 https://www.devze.com 2023-04-06 10:55 出处:网络
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

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"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号