52

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.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
Boe Jingles
  • 545
  • 1
  • 4
  • 6
  • related: [Find if 24 hrs have passed between datetimes - Python](https://stackoverflow.com/q/26313520/4279) – jfs Jan 17 '18 at 21:58

6 Answers6

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
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • 15
    don't use local time (`datetime.now()`). [`Use datetime.utcnow()`](http://stackoverflow.com/a/12126443/4279). – jfs Aug 25 '12 at 23:47
16

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 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 06 '21 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.

debsim
  • 582
  • 4
  • 19
Santiago Alessandri
  • 6,630
  • 30
  • 46
  • That won't calculate the total the difference in seconds (the number of seconds maxes out at the number of seconds in a day) – David Robinson Aug 25 '12 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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
4
import datetime
born = datetime.date(2002, 10, 31)
today = datetime.date.today()
age = today - born

print(age.total_seconds())

Output: 463363200.0

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Abacito
  • 141
  • 7
4

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
Mohamed Fathallah
  • 1,274
  • 1
  • 15
  • 17