如果你使用Ruby 1.9,Time.parse
了解您的格式就好:
ruby-1.9.2-p180 :003 > t = Time.parse("Mon, 10 Oct 2011 20:13:10 +0000")
=> 2011-10-10 23:13:10 +0300
ruby-1.9.2-p180 :004 > t.month
=> 10
ruby-1.9.2-p180 :005 > t.strftime("%d-%m-%Y")
=> "10-10-2011"
在1.8,DateTime
也有类似的方法:
irb(main):004:0> require 'date'
=> true
irb(main):006:0> t = DateTime.parse("Mon, 10 Oct 2011 20:13:10 +0000")
=> #<DateTime: 2011-10-10T20:13:10+00:00 (21218503759/8640,0/1,2299161)>
irb(main):007:0> t.month
=> 10
如果你想自己实现distance_of_time_in_words ,最好的出发点可能是检查出its source(点击该页面上的查看源代码链接)。
非常好,谢谢你解释得那么确切。 – yoamomonstruos