[英]Difference between timestamps in Arrow
How would I get Arrow to return the difference in hours between two timestamps? 我如何让Arrow返回两个时间戳之间的小时差异?
Here's what I have: 这就是我所拥有的:
difference = arrow.now() - arrow.get(p.create_time())
print(difference.hour)
p.create_time()
being the timestamp of the create time of a currently running process. p.create_time()
是当前正在运行的进程的创建时间的时间戳。
Returns: 返回:
AttributeError: 'datetime.timedelta' object has no attribute 'hour'
Edit: I don't want the total time in all three formats, I want it as a remainder eg. 编辑:我不想要所有三种格式的总时间,我希望它作为余数,例如。 "3 days, 4 hours, 36 minutes" not "3 days, 72 hours, 4596 minutes"
“3天,4小时,36分钟”不是“3天,72小时,4596分钟”
Given 2 dates that are formatted from a string to arrow
type. 给定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
The difference is a datetime.timedelta
data type. 区别在于
datetime.timedelta
数据类型。
>>> print type(diff)
<type 'datetime.timedelta'>
And results in: 并导致:
>>> print diff
692 days, 18:37:32
To get it formatted such that you would have D days, H hours, M minutes, S seconds
you would get the days separately, and then using divmod
function get the other information. 要使其格式化,以便您有
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
The result would be: 结果将是:
>>> print days, " Days, ", hours, " Hours, ", minutes, " Minutes, ", seconds, " Second"
692 Days, 18 Hours, 37 Minutes, 32 Second
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.