如何在Ruby中将此字符串转换为DateTime

时间:2014-02-27 13:11:26

标签: ruby-on-rails ruby datetime

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

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

我还在顶部包含了这些内容:

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

1 个答案:

答案 0 :(得分:2)

使用Date::strptime方法:

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