Ruby解析日期字符串
我需要将下面提到的日期字符串解析为std日期格式。
在ruby中是否有内置函数可以解析类似的东西,
2011年12月9日至2011-12-09
已经提到了Date.parse
。
我更喜欢Date.strptime
。 这种方法是反向strftime 。
Date.parse
是一个(可能是好的)猜测,当你知道你期望的格式时, Date.strptime
可以解析每个日期。
例:
require 'date' puts Date.strptime('December 09, 2011', '%B %d, %Y')
或者,如果您有另一种格式,其中Date.parse
失败 :
require 'date' puts Date.strptime("28-May-10", "%d-%b-%y") #2010-05-28
使用Date::parse
执行以下操作:
require 'date' Date.parse('December 09, 2011').to_s # => "2011-12-09"