简体   繁体   中英

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM