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'
You can use simple date arithmetic to find the number of days between two dates in Python. Define the 2 dates between which you want to find the difference in days. Then subtract these dates to get a timedelta object and examine the day's property of this object to get the required result.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With