Python日期时间格式没有零填充 [英] Python datetime formatting without zero-padding
问题描述
现在使用的格式是:
有没有打印Python日期格式的日期和时间不会使用零填充的格式?
mydatetime.strftime('%m /%d /%Y%I:%M%p')
结果: 2012年2月29日下午05:03
2/29/2012 5:03 PM
什么格式将月份表示为'2'而不是'02',时间为'5:03 PM '而不是'05:03PM'
datetime.strftime()
的结果: s = mydatetime.strftime('%m /%d /%Y%I:%M%p')lstrip(0) .replace(0,)
(也就是说,如果您决定这样做真的值得这样做,为什么不简单地把零置入?)
Is there a format for printing Python datetimes that won't use zero-padding on dates and times?
Format I'm using now:
mydatetime.strftime('%m/%d/%Y %I:%M%p')
Result: 02/29/2012 05:03PM
Desired: 2/29/2012 5:03PM
What format would represent the month as '2' instead of '02', and time as '5:03PM' instead of '05:03PM'
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", " ")
(That is, if you decide that doing so is really worth the trouble. Why not simply leave the zeros in?)
这篇关于Python日期时间格式没有零填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!