我将如何获得箭头以返回两个时间戳之间的小时差?
这就是我所拥有的:
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 days, H hours, M minutes, S seconds
,您将分别获得日子,然后使用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