我如何格式化大于24小时的timedelta以在Python中仅包含小时显示?
>>> import datetime >>> td = datetime.timedelta(hours=36, minutes=10, seconds=10) >>> str(td) '1 day, 12:10:10' # my expected result is: '36:10:10'
我通过以下方式达到目标:
import datetime td = datetime.timedelta(hours=36, minutes=10, seconds=10) seconds = td.total_seconds() hours = seconds // 3600 minutes = (seconds % 3600) // 60 seconds = seconds % 60 str = '{}:{}:{}'.format(int(hours), int(minutes), int(seconds)) >>> print(str) 36:10:10
有没有更好的办法?