convert string to Time?

P

Paul Brannan

How can I convert a string such as '2007-01-31 12:22:26' to a Time
object?

irb(main):001:0> require 'time'
=> true
irb(main):002:0> Time.parse('2007-01-31 12:22:26')
=> Wed Jan 31 12:22:26 EST 2007
 
T

Tom Werner

Chris said:
How can I convert a string such as '2007-01-31 12:22:26' to a Time
object?

Chronic can do that and a whole lot more (in case you need to)!

require 'chronic'

Chronic.parse('2007-01-31 12:22:26')
=> Wed Jan 31 12:22:26 -0800 2007

Chronic.parse('today at 12:22.26')
=> Wed Jan 31 12:22:26 -0800 2007

http://chronic.rubyforge.org

Tom
 
K

_Kevin

Look athttp://whytheluckystiff.net/ruby/pickaxe/

.. see Time.mktime

.. first you should split(' ') ya string and then split the result,
order da stuff and put it into mktime...

Am 31.01.2007 um 22:20 schrieb Chris McMahon:

the ruby-units gem also has a helper for this...

'2007-01-31 12:22:26'.to_time #=> Wed Jan 31 12:22:26 EST 2007

_Kevin
 
C

Christopher Brown

the ruby-units gem also has a helper for this...

'2007-01-31 12:22:26'.to_time #=3D> Wed Jan 31 12:22:26 EST 2007

_Kevin
Time. parse seems to do the trick too...

irb(main):003:0> Time.parse("2007-01-31 12:22:26")
=3D> Wed Jan 31 12:22:26 SAST 2007
 

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,991
Messages
2,570,212
Members
46,800
Latest member
Tobi1987

Latest Threads

Top