我有一个想要在网站上显示的 python datetime 对象,但是时间以 hh:mm:ss 格式显示,我想以 hh:mm 格式显示它。
我尝试使用替换方法如下:
message.timestamp.replace(second='0', microsecond=0)
Run Code Online (Sandbox Code Playgroud)
然而,这不会变红它只是用 hh:mm:00 替换它
使用strftime()函数
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 12, 27, 11, 14, 37, 137010)
>>> now = datetime.now()
>>> now.strftime("%H:%M")
'11:15'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7710 次 |
| 最近记录: |