Date Time Difference¶
from datetime import datetime
from collections import namedtuple
dhms = namedtuple('dhms', ( 'days', 'hours', 'minutes', 'seconds' ))
X = lambda _: datetime.strptime(_, '%Y-%m-%d %H:%M:%S')
Y = lambda a,b: dhms( (b-a).days, (b-a).seconds // 3600, ((b-a).seconds // 60) % 60, (b-a).seconds % 60 )
Example Usage¶
The result is 2 days, 23 hours, 15 minutes and 57 seconds.
>>> a = X('2021-01-29 13:30:21')
>>> b = X('2021-02-01 12:46:18')
>>> Y(a,b)
dhms(days=2, hours=23, minutes=15, seconds=57)