Arrow中时间戳的区别

时间:2016-12-24 02:07:49

标签: python datetime arrow-python

我如何让Arrow返回两个时间戳之间的小时差异?

这就是我所拥有的:

DECLARE SUB GetFiles (Var$)
REM list files in current directory
C$ = "C:*.*"
CALL GetFiles(C$)
END

SUB GetFiles (Var$)
Var2$ = "dir " + Var$ + " > tempfile.dir"
SHELL Var2$
OPEN "tempfile.dir" FOR INPUT SHARED AS #1
DO UNTIL EOF(1)
    LINE INPUT #1, X$
    PRINT X$
LOOP
END SUB

difference = arrow.now() - arrow.get(p.create_time()) print(difference.hour) 是当前正在运行的进程的创建时间的时间戳。

返回:

p.create_time()

编辑:我不想要所有三种格式的总时间,我希望它作为余数,例如。 “3天,4小时,36分钟”不是“3天,72小时,4596分钟”

1 个答案:

答案 0 :(得分:9)

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