如何用datetime.strptime来计算期间(AM / PM)? [英] How can I account for period (AM/PM) with datetime.strptime?
本文介绍了如何用datetime.strptime来计算期间(AM / PM)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具体来说,我有代码可以简化为:
Specifically I have code that simplifies to this:
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解析方法在解析日期或者我做某事愚蠢时,只是忽略期间说明符?
What gives? Does Python just ignore the period specifier when parsing dates or am I doing something stupid?
推荐答案
Python time.strftime
docs 说:
The Python time.strftime
docs say:
当与strptime()函数一起使用时,
%p
只有
会影响输出小时字段,如果%I
指令用于解析小时
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
使其正常工作。
Sure enough, changing your %H
to %I
makes it work.
这篇关于如何用datetime.strptime来计算期间(AM / PM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文