How to format datetime in ruby with Sinatra and Twitter Gem
I'm completely new to ruby and I'm trying to format the created_at field pulled in from the Twitter gem. I know in rails you can use time_ago_in_words for rails, but I was wondering how you can do this in plain ruby. Infact I'm wondering how you can format a date which is in this for开发者_如何学Gomat to start with "Mon, 10 Oct 2011 20:13:10 +0000". Is this the standard date format?
Any help would be brilliant.
If you're using Ruby 1.9, Time.parse
understands your format just fine:
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"
In 1.8, DateTime
has a similar method:
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
If you want to implement distance_of_time_in_words by yourself, the best starting point is probably to check out its source (click the view source link on that page).
精彩评论