Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract two dates to give a timedelta

I'm trying to get a value from one of my database values, which will be given by subtracting the purchase date from today's date. I've written my code this way:

delta = datetime.now() - item.purchase_date

But this gives me this error:

unsupported operand type(s) for -: 'datetime.datetime' and 'datetime.date'

If I use datetime.datetime.now() this doesn't work. What am I missing. Thanks.

like image 255
Stephen Avatar asked Feb 10 '10 15:02

Stephen


1 Answers

you need to use date.today or datetime.now().date() instead of datetime.now:

>>> import datetime
>>> datetime.date.today()
datetime.date(2010, 2, 10)
>>> datetime.datetime.now().date()
datetime.date(2010, 2, 10)
like image 92
SilentGhost Avatar answered Oct 14 '22 18:10

SilentGhost