Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
May 2 ’11

Parse a string into a DateTime object. Controlled.

Just as you can format a string from a Time object with Time#strftime, you can also parse a string in a defined format into a DateTime or Date object, using DateTime#strptime or Date#strptime respectively (Date#strptime only creates a date without the time, though).

require 'date'

parsed_time = DateTime.strptime('03/05/2010 14:25:00', '%d/%m/%Y %H:%M:%S')

parsed_time.to_s
=> "2010-05-03T14:25:00+00:00"

See the docs for more info: DateTime#strptime and Date#strptime.

2 notes 0 comments

  1. rubyquicktips posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus