具体来说,我有代码可以简化为:
from datetime import datetime
date_string = '2009-11-29 03:17 PM'
format = '%Y-%m-%d %H:%M %p'
my_date = datetime.strptime(date_string, format)
# This prints '2009-11-29 03:17 AM'
print my_date.strftime(format)
什么给了? Python 在解析日期时是否只是忽略了句点说明符,还是我在做一些愚蠢的事情?
最佳答案
python time.strftime
docs说:
When used with the strptime() function, the
%p
directive only affects the output hour field if the%I
directive is used to parse the hour.
果然,把你的 %H
改成 %I
就可以了。
https://stackoverflow.com/questions/1759455/