You could use this function:
import datetime
import calendar
def difference(start, end):
years = end.year - start.year
months = end.month - start.month
days = end.day - start.day
hours = end.hour - start.hour
minutes = end.minute - start.minute
seconds = end.second - start.second
if seconds < 0:
minutes -= 1
seconds += 60
if minutes < 0:
hours -= 1
minutes += 60
if hours < 0:
days -= 1
hours += 24
if days < 0:
months -= 1
days += calendar.monthrange(start.year, start.month)[1]
if months < 0:
years -= 1
months += 12
return {
'years': years,
'months': months,
'days': days,
'hours': hours,
'minutes': minutes,
'seconds': seconds
}
birthday = datetime.datetime(1996,8,30)
diff = difference(birthday, datetime.datetime.now())
print(diff)
This counts the years (including leap years) as they occur between the two dates.
The difference in days is either the net difference of the day numbers, or -- if the current date has a smaller day number than the birth date -- then the number of days remaining in the month of birth are added to the current day number. This can be a bit arbitrary. Suppose tomorrow is my birthday (and the birthday is not on the first of the month), then the output will have 11 for the months as expected, but either 27, 28, 29 or 30 for the days component, depending how many days the month of birth has...