49

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.

1

6 Answers 6

55
from datetime import datetime

birthday = datetime(1988, 2, 19, 12, 0, 0)
diff = datetime.now() - birthday
print diff
# 8954 days, 7:03:45.765329
1
15

Use UTC time otherwise age in seconds can go backwards during DST transition:

from datetime import datetime

born = datetime(1981, 12, 2) # provide UTC time
age = datetime.utcnow() - born
print(age.total_seconds())

You also can't use local time if your program runs on a computer that is in a different place (timezone) from where a person was born or if the time rules had changed in this place since birthday. It might introduce several hours error.

If you want to take into account leap seconds then the task becomes almost impossible.

1
  • total_seconds() was a better answer for me as just using .seconds would give me a positive answer regardless. So not good for an if x > 0: condition
    – Josh
    Jul 6, 2021 at 15:24
8

When substracting two datetime objects you will get a new datetime.timedelta object.

from datetime import datetime
x = datetime.now()
y = datetime.now()
delta = y - x

It will give you the time difference with resolution to microsencods.

For more information take a look at the official documentation.

1
  • That won't calculate the total the difference in seconds (the number of seconds maxes out at the number of seconds in a day) Aug 25, 2012 at 23:07
5

Create a datetime.datetime from your date:

datetime.datetime.combine(birthdate, datetime.time())

Now you can subtract it from datetime.datetime.now().

>>> from datetime import date, datetime, time
>>> bday = date(1973, 4, 1)
>>> datetime.now() - datetime.combine(bday, time())
datetime.timedelta(14392, 4021, 789383)
>>> print datetime.now() - datetime.combine(bday, time())
14392 days, 1:08:13.593813
4
import datetime
born = datetime.date(2002, 10, 31)
today = datetime.date.today()
age = today - born

print(age.total_seconds())

Output: 463363200.0

3

Since DateTime.DateTime is an immutable type method like these always produce a new object the difference of two DateTime object produces a DateTime.timedelta type:

from datetime import date,datetime,time,timedelta
dt=datetime.now()
print(dt)
dt2=datetime(1997,7,7,22,30)
print(dt2)
delta=dt-dt2
print(delta)

print(int(delta.days)//365)
print(abs(12-(dt2.month-dt.month)))
print(abs(dt.day))

The output timedelta(8747,23:48:42.94) or what ever will be days when u test the code indicates that the time delta encodes an offset of 8747 days and 23hour and 48 minute ...

The Output

   2021-06-19 22:27:36.383761
    1997-07-07 22:30:00
    8747 days, 23:57:36.383761
    23  Year
    11  Month
    19  Day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.