问题描述
是否有一种打印 python 日期时间的格式,它不会在日期和时间上使用零填充?
is there a format for printing python datetimes that won't use zero-padding on dates and times?
我现在使用的格式:
mydatetime.strftime('%m/%d/%y %i:%m%p')
结果: 02/29/2012 05:03pm
希望: 2012 年 2 月 29 日下午 5:03
result: 02/29/2012 05:03pm
desired: 2/29/2012 5:03pm
什么格式将月份表示为2"而不是02",时间表示为5:03pm"而不是05:03pm"
what format would represent the month as '2' instead of '02', and time as '5:03pm' instead of '05:03pm'
推荐答案
datetime.strftime() 可用的格式化选项将全部填零.你当然可以滚动你自己的格式化函数,但在这种情况下最简单的尊龙凯时的解决方案可能是对 datetime.strftime() 的结果进行后处理:
the formatting options available with datetime.strftime() will all zero-pad. you could of course roll you own formatting function, but the easiest solution in this case might be to post-process the result of datetime.strftime():
s = mydatetime.strftime('%m/%d/%y %i:%m%p').lstrip("0").replace(" 0", " ")