python将timedelta转换为有意义的字符串,如“1小时”
我想将python timedelta对象转换为易于阅读的有意义字符串 例如:python将timedelta转换为有意义的字符串,如“1小时”,python,python-3.x,timedelta,Python,Python 3.x,Timedelta,我想将python timedelta对象转换为易于阅读的有意义字符串 例如: 0 1:0:0 -> 1 hours 0 0:5:0. -> 5 minutes 2 0:0:0. ->. 2 days 有什么办法吗 感谢实际上,timedelta类直接为您提供了天、秒、微秒属性。但是为了得到小时和分钟,你可以自己做一些简单的计算 import datetime d1 = datetime.datetime(2012, 4, 20, second=10, microsecon
0 1:0:0 -> 1 hours
0 0:5:0. -> 5 minutes
2 0:0:0. ->. 2 days
有什么办法吗
感谢实际上,timedelta类直接为您提供了天、秒、微秒属性。但是为了得到小时和分钟,你可以自己做一些简单的计算
import datetime
d1 = datetime.datetime(2012, 4, 20, second=10, microsecond=14)
d2 = datetime.datetime(2012, 3, 20, second=8, microsecond=4)
delta = d1 - d2
print(delta)
print(delta.days)
print(delta.seconds)
print(delta.microseconds)
将秒除以60得到分钟,将秒除以3600得到小时,您可能想看看python库:
>>> humanize.naturaldelta(dt.timedelta(seconds=1001))
'16 minutes'
这回答了你的问题吗?