Использование примера datetime
>>> from datetime import datetime
>>> then = datetime(2012, 3, 5, 23, 8, 15) # Random date in the past
>>> now = datetime.now() # Now
>>> duration = now - then # For build-in functions
>>> duration_in_s = duration.total_seconds() # Total number of seconds between dates
Продолжительность в годах
>>> years = divmod(duration_in_s, 31556926)[0] # Seconds in a year=31556926.
Продолжительность в днях
>>> days = duration.days # Build-in datetime function
>>> days = divmod(duration_in_s, 86400)[0] # Seconds in a day = 86400
Продолжительность в часах
>>> hours = divmod(duration_in_s, 3600)[0] # Seconds in an hour = 3600
Продолжительность в минутах
>>> minutes = divmod(duration_in_s, 60)[0] # Seconds in a minute = 60
Продолжительность в секундах
>>> seconds = duration.seconds # Build-in datetime function
>>> seconds = duration_in_s
Длительность в микросекундах
>>> microseconds = duration.microseconds # Build-in datetime function
Общая продолжительность между двумя датами
>>> days = divmod(duration_in_s, 86400) # Get days (without [0]!)
>>> hours = divmod(days[1], 3600) # Use remainder of days to calc hours
>>> minutes = divmod(hours[1], 60) # Use remainder of hours to calc minutes
>>> seconds = divmod(minutes[1], 1) # Use remainder of minutes to calc seconds
>>> print("Time between dates: %d days, %d hours, %d minutes and %d seconds" % (days[0], hours[0], minutes[0], seconds[0]))
или просто:
>>> print(now - then)