Python datetime formatting without zero-padding(没有零填充的 Python 日期时间格式)
问题描述
是否有一种打印 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", " ")
这篇关于没有零填充的 Python 日期时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有零填充的 Python 日期时间格式
基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
