strptime - 无效的日期错误

时间:2012-10-29 02:57:31

标签: ruby-on-rails ruby

这应该是超级简单的,但我很难过:

require 'time'
# works fine, prints "2/20/2012 6:46:00 PM"
puts Time.new(2012,2,20,18,46,0).strftime('%-m/%-d/%Y %-l:%M:%S %p')
test_date = '2/20/2012 6:46:00 PM'
# Invalid date error -- why??
# What am I doing wrong in my usage of strptime?
last_login = DateTime.strptime(test_date, '%-m/%-d/%Y %-l:%M:%S %p')

谢谢!

1 个答案:

答案 0 :(得分:5)

似乎strptimestrftime之间存在一点差异。我认为您-中不需要strptime

test_date = '2/20/2012 6:46:00 PM'
last_login = DateTime.strptime(test_date, '%m/%d/%Y %l:%M:%S %p')