如何让 Arrow 返回两个时间戳之间的小时差?
这是我所拥有的:
difference = arrow.now() - arrow.get(p.create_time())
print(difference.hour)
p.create_time()
是当前运行进程的创建时间的时间戳。
返回:
AttributeError: 'datetime.timedelta' object has no attribute 'hour'
编辑:我不想要所有三种格式的总时间,我想要它作为余数,例如。 “3 天 4 小时 36 分钟”而不是“3 天 72 小时 4596 分钟”
最佳答案
给定 2 个从字符串格式化为 arrow
类型的日期。
>>> date_1 = arrow.get('2015-12-23 18:40:48','YYYY-MM-DD HH:mm:ss')
>>> date_2 = arrow.get('2017-11-15 13:18:20','YYYY-MM-DD HH:mm:ss')
>>> diff = date_2 - date_1
区别是datetime.timedelta
数据类型。
>>> print type(diff)
<type 'datetime.timedelta'>
结果是:
>>> print diff
692 days, 18:37:32
要将其格式化为 D 天、H 小时、M 分钟、S 秒
,您需要分别获取天数,然后使用 divmod
函数获取其他信息。
>>> days = diff.days # Get Day
>>> hours,remainder = divmod(diff.seconds,3600) # Get Hour
>>> minutes,seconds = divmod(remainder,60) # Get Minute & Second
结果是:
>>> print days, " Days, ", hours, " Hours, ", minutes, " Minutes, ", seconds, " Second"
692 Days, 18 Hours, 37 Minutes, 32 Second
关于python - Arrow 中的时间戳之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309708/