如何使用strftime来计算期间(AM / PM)? [英] How can I account for period (AM/PM) using strftime?
本文介绍了如何使用strftime来计算期间(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 time.strftime
文档说:
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.
这篇关于如何使用strftime来计算期间(AM / PM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文