59

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

7 Answers 7

64
from datetime import datetime

birthday = datetime(1988, 2, 19, 12, 0, 0)
diff = datetime.now() - birthday
print diff
# 8954 days, 7:03:45.765329
Sign up to request clarification or add additional context in comments.

3 Comments

don't use local time (datetime.now()). Use datetime.utcnow().
2024 Update ;) Don't use datetime.utcnow(), use datetime.now(timezone.utc) ... MAYBE? Point is datetime.utcnow() is being deprecated
I believe you want to use datetime.now(datetime.UTC)
17

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 Comment

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
10

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 Comment

That won't calculate the total the difference in seconds (the number of seconds maxes out at the number of seconds in a day)
6

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

Comments

5

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

Comments

4
import datetime
born = datetime.date(2002, 10, 31)
today = datetime.date.today()
age = today - born

print(age.total_seconds())

Output: 463363200.0

Comments

0
from datetime import datetime

birthday = datetime(2024, 11, 12, 6, 1, 0) # Should be in UTC
cur_date = datetime.now(datetime.UTC) # Time in UTC
elapsed_time = cur_date - birthday
print(elapsed_time)

I know this is late, but:

  • Code is basically other answers compiled together to make it a better answer.
  • This code works in . The current accepted answer only works in Python 2 because of the print statement.

Helpful documentation:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.