Sunday, January 30, 2011

convert string to Time rails


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
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

No comments:

Post a Comment