Calculate date time difference python

前端 未结 1 1403
梦如初夏
梦如初夏 2021-01-25 07:00

I am writing timediff function to calculate the time (seconds) difference between 2 giving date time

def timediff(time1, time2):
    timeformat = \'%d%b%Y:%H:%M:         


        
相关标签:
1条回答
  • 2021-01-25 07:27

    You need to use total_seconds() and not seconds:

    >>> import datetime
    >>> f = '%d%b%Y:%H:%M:%S'
    >>> t1 = '01MAR2016:07:11:53'
    >>> t2 = '02MAR2016:07:11:53'
    >>> d1 = datetime.datetime.strptime(t1, f)
    >>> d2 = datetime.datetime.strptime(t2, f)
    >>> print(d2-d1)
    1 day, 0:00:00
    >>> print((d2-d1).total_seconds())
    86400.0
    >>> print((d2-d1).seconds)
    0
    
    0 讨论(0)
提交回复
热议问题