没有零填充的 Python 日期时间格式
- 作者: 相机定格咔嚓
- 来源: 51数据库
- 2022-12-05
问题描述
是否有一种打印 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", " ")
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
