Reputation: 3628
How to i convert "04/16/2012 03:44:26" string to datetime object in ruby?
I knew that "20120416" can be converted to datetime object by DateTime.parse(srting).
Please some one help me in converting "04/16/2012 03:44:26" to datetime object in ruby. And i need to add x minute to the above datetime object.
Upvotes: 2
Views: 4733
Reputation: 2562
(Time.parse("04/16/2012 03:44:26") + 10.minutes).to_datetime
UPDATE
Works even in Ruby 1.8.7, but you need to require ActiveSupport (used automatically in Rails).
Upvotes: 3
Reputation: 281
You need to specify the format using DateTime#strptime:
DateTime.strptime("04/16/2012 03:44:26", '%m/%d/%Y %H:%M:%S')
See: http://ruby-doc.org/stdlib-1.8.7/libdoc/date/rdoc/DateTime.html#method-c-strptime
You can add x minutes like so: + (x / (24 * 60.0))
.
If you are using Rails or have no problems using ActiveSupport, you can just do:
require 'active_support'
new_datetime = datetime + 10.minutes
Upvotes: 3