Reverting date from Time.to_s

U

Utsav Gupta

I receive the dates from Twitter in the following format "Mon Mar 22
04:42:17 +0000 2010"

Now how can I create a time object from this string... ie Obj.year=2010,
Obj.month=3... etc etc


Regards

Utsav Gupta
 
C

Chuck Remes

I receive the dates from Twitter in the following format "Mon Mar 22
04:42:17 +0000 2010"

Now how can I create a time object from this string... ie Obj.year=2010,
Obj.month=3... etc etc

Quite simple.

cremes$ irb
ruby-1.9.1-p378 > require 'time'
=> true
ruby-1.9.1-p378 > s = "Mon Mar 22 04:42:17 +0000 2010"
=> "Mon Mar 22 04:42:17 +0000 2010"
ruby-1.9.1-p378 > t = Time.parse s
=> 2010-03-21 23:42:17 -0500
ruby-1.9.1-p378 > t.month
=> 3
ruby-1.9.1-p378 > t.year
=> 2010

Note that Time.parse creates your time object with the local timezone. You can still access zulu time by using the #utc method on that object.

cr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top