Skip to content
Advertisement

Subtracting Dates With Python

I’m working on a simple program to tell an individual how long they have been alive.

I know how to get the current date, and get their birthday. The only problem is I have no way of subtracting the two, I know a way of subtracting two dates, but unfortunately it does not include hours, minutes, or seconds.

I am looking for a method that can subtract two dates and return the difference down to the second, not merely the day.

Advertisement

Answer

from datetime import datetime

birthday = datetime(1988, 2, 19, 12, 0, 0)
diff = datetime.now() - birthday
print diff
# 8954 days, 7:03:45.765329
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement