views:

361

answers:

2

In Python, I'm attempting to retrieve the date/time that is exactly 30 days (30*24hrs) into the past. At present, I'm simply doing:

>>> import datetime
>>> start_date = datetime.date.today() + datetime.timedelta(-30)

Which returns a datetime object, but with no time data:

>>> start_date.year
2009
>>> start_date.hour
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.date' object has no attribute 'hour'
+7  A: 

You want to use a datetime object instead of just a date object:

start_date = datetime.datetime.now() + datetime.timedelta(-30)

date just stores a date and time just a time. datetime is a date with a time.

sth
Ah, nailed. Thanks.
Nick Sergeant
A: 

date <> datetime

Trey Stout
It is actually. They aren't the same thing.
Trey Stout
This is Python, not Pascal. <> is deprecated. :(
Devin Jeanpierre
Use `!=` instead of `<>` in Python.
J.F. Sebastian
Further posts on the merits of not equal operators != helpful.
Trey Stout
!= > <>, imho .
Claudiu