Parsing string dates in ruby such as “28-May-10”

假装没事ソ 提交于 2019-12-04 04:29:40

问题


I tried parsing using

Date.parse("28-May-10").to_s

Returns 0010-5-28 (which is 2000 years off!)

How can I get ruby to interpret the two digit year properly.

There are plenty of string to date conversion tricks out there on google but most handle digit months as opposed to "May".


回答1:


I prefer Date.strptime for this task:

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


来源:https://stackoverflow.com/questions/9727898/parsing-string-dates-in-ruby-such-as-28-may-10

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