Pavan Katepalli
Pavan Katepalli

Reputation: 2464

How to convert this string into DateTime in Ruby

string = "02/27/2014 08:01:11"
DateTime.parse(string) # failed

and

DateTime.strptime(string) # failed
(string).to_datetime # failed

I also included these at the top:

require 'date'
require 'active_support/core_ext/string/conversions'

Upvotes: 0

Views: 62

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Use Date::strptime method :

require 'date'

s =  "02/27/2014 08:01:11"
Date.strptime(s,"%m/%d/%Y %H:%M:%S") 
# => #<Date: 2014-02-27 ((2456716j,0s,0n),+0s,2299161j)>

Upvotes: 2

Related Questions