views:

336

answers:

1

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.

+4  A: 

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)
SilentGhost
This gives me 'method_descriptor' object has no attribute 'today'
Stephen
you have a mess with your imports it seems, I've posted an example of how it works.
SilentGhost
datetime.now().date() works fine. date.today works only from the python shell.
Stephen
it works only from python shell because you're not importing it.
SilentGhost