0
def work(dat1, dat2):

>>> from datetime import date
>>> work(date(1969, 10, 5), date(1975, 10, 14))

>>> x = dat1 - dat2
>>> return x
datetime.timedelta(2200)

How can I simply return the value without 'datetime.timedelta' ? In this case I want to return 2200.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Victor
  • 65
  • 7
  • 1
    https://docs.python.org/2/library/datetime.html#datetime.timedelta – jonrsharpe Nov 13 '15 at 13:25
  • in general, (if you need more precise result) there could be time zone issues, see [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Nov 14 '15 at 01:03

2 Answers2

2

If you only need to get the difference of dates (without time), then retrieving the days attribute is enough:

(date(1975, 10, 14) - date(1969, 10, 5)).days

If you need days with floating point precision then I would calculate this using total_seconds() as:

(dt1 - dt2).total_seconds() / (3600*24)
Kon Pal
  • 546
  • 1
  • 3
  • 13
1

What you are looking for, is the distance in days between the two dates? In that case return x.days should do the trick. For more info on timedelta objects, read https://docs.python.org/2/library/datetime.html#timedelta-objects

Kendas
  • 1,963
  • 13
  • 20