【问题标题】:Difference between timestamps in Arrow箭头中时间戳之间的差异
【发布时间】:2017-05-09 15:31:59
【问题描述】:

如何让 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 分钟”

【问题讨论】:

标签: python datetime arrow-python


【解决方案1】:

给定 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多