简体   繁体   English

strptime - 无效的日期错误

[英]strptime — Invalid Date Error

This should be super simple, but I am stumped: 这应该是超级简单的,但我很难过:

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

Thanks! 谢谢!

It seems there is a little difference between strptime and strftime . 似乎strptimestrftime之间有一点区别。 I don't think you need - in strptime . 我觉得你不需要-strptime

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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