开发者

Make Date#parse assume a US format instead of an EU format

开发者 https://www.devze.com 2023-01-17 02:56 出处:网络
With 1.9.2p0, Date#parse assumes an UE format.Check out format.rb, line: 1042 if you don\'t believe me.

With 1.9.2p0, Date#parse assumes an UE format. Check out format.rb, line: 1042 if you don't believe me.

Anyways, how can I make it 开发者_开发知识库assume a US format, so that:

> Date.parse("10/4/2010")
 => Mon, 04 Oct 2010

Instead of April 10th.

I've tried this:

class Date
  def _parse_eu(str,e)
    _parse_us(str,e)
  end
end

but no luck. Any other ideas?


Date.strptime is what you want but unfortunately it doesn't look like the documentation has the date formatting strings. I got the following to work based on Googling for the format strings:

1.9.2 > d = Date.strptime("10/4/2010", "%m/%d/%Y")
=> #<Date: 2010-10-04 (4910947/2,0,2299161)> 
1.9.2 > d.day
=> 4 
1.9.2 > d = Date.strptime("10/4/2010", "%d/%m/%Y")
=> #<Date: 2010-04-10 (4910593/2,0,2299161)> 
1.9.2 > d.day
=> 10


You might want to check out strptime instead.

0

精彩评论

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

关注公众号