使日期#解析采用美国格式而不是欧盟格式 [英] Make Date#parse assume a US format instead of an EU format

查看:192
本文介绍了使日期#解析采用美国格式而不是欧盟格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用1.9.2p0,Date#解析假定为UE格式。查看format.rb,行:1042如果你不相信我。

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

而不是4月10日。

我已经尝试过:

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

但没有运气。任何其他想法?

but no luck. Any other ideas?

推荐答案

Date.strptime 是你想要的,但不幸的是它看起来不像文档具有日期格式化字符串。我根据Googling的格式字符串获得以下工作:

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

这篇关于使日期#解析采用美国格式而不是欧盟格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆