开发者

Chronic parsing of month/day

I am using Chronic to parse dates and it is parsing the string '8/15' as August 16, 2015.

Any ideas on how I could get Chronic to recognize 8/15 as August 15, cu开发者_运维技巧rrent year?

Here is the test code:

Chronic.parse('8/15') #=> 'Sun Aug 16 12:00:00 -0400 2015'


As an option:

irb(main):016:0> Chronic.parse("#{Time.now.year}/8/15")
=> 2011-08-15 12:00:00 +0300


The best way to do this would be using the normal DateTime parsing provided by Ruby. As an example you can parse the date by calling:

ruby-1.9.2-p290 :001 > require 'date'
 => true 
ruby-1.9.2-p290 :002 > DateTime.strptime("8/15", "%m/%d")
 => #<DateTime: 2011-08-15T00:00:00+00:00 (4911577/2,0/1,2299161)> 
ruby-1.9.2-p290 :003 > 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜