Ruby parse date string [closed]

烂漫一生 提交于 2019-12-09 07:48:22

问题


I need to parse a date string mentioned below to std date format.

Is there a built in function in ruby that would parse something like,

December 09, 2011 to 2011-12-09


回答1:


Date.parse is already mentioned.

I prefer Date.strptime. This methods is a reverse strftime.

Date.parse is a (maybe good) guess, with Date.strptime you can parse each date, when you know which format you expect.

Example:

require 'date'
puts  Date.strptime('December 09, 2011', '%B %d, %Y')

Or if you have another format where Date.parse fails:

require 'date'
puts Date.strptime("28-May-10", "%d-%b-%y") #2010-05-28



回答2:


Do as below using Date::parse:

require 'date'
Date.parse('December 09, 2011').to_s # => "2011-12-09"


来源:https://stackoverflow.com/questions/21391953/ruby-parse-date-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!