Skip to content Skip to sidebar Skip to footer

Calculate Date Time Difference Python

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:%S' #time1='01MA

Solution 1:

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

Post a Comment for "Calculate Date Time Difference Python"