alw*_*btc 2 python string time date
我有一个3465小时的timedelta对象
timedelta(hours=3457)
Run Code Online (Sandbox Code Playgroud)
我想以"HH:MM"格式表示它,即"3457:00"
我做:
from datetime import datetime
hours = timedelta(hours=3457)
hours_string = time.strftime("%H:%M", time.gmtime(hours.seconds))
print hours_string
"01:00"
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到"3457:00"?
请注意,3457:00是一种荒谬的格式."小时 - 冒号 - 分钟"格式用于日期和时间,然后小时不能合理地高于23.更合理的格式是:3457h 0m.
你可以这样得到它:
from datetime import timedelta
delta = timedelta(hours=3457)
minutes, seconds = divmod(delta.seconds, 60)
hours, minutes = divmod(minutes, 60)
hours += delta.days * 24
print '%sh %sm' % (hours, minutes)
Run Code Online (Sandbox Code Playgroud)
当然,更简单的方法是:
from datetime import timedelta
delta = timedelta(hours=3457)
print delta
Run Code Online (Sandbox Code Playgroud)
但这会给你"144天,1:00:00",这是一种理智的格式,但不是你想要的.
| 归档时间: |
|
| 查看次数: |
438 次 |
| 最近记录: |