'datetime.date' object has no attribute 'timedelta'
时间: 2023-09-13 09:04:49 浏览: 299
C# DateTime.ToString根据不同语言生成相应的时间格式
This error occurs when you try to use the timedelta method on a datetime.date object. The timedelta method is only available for datetime.datetime objects.
To fix this error, you can convert the datetime.date object to a datetime.datetime object using the datetime.combine() method. Here's an example:
```
import datetime
date_obj = datetime.date(2021, 10, 1)
datetime_obj = datetime.datetime.combine(date_obj, datetime.datetime.min.time())
# now you can use the timedelta method
delta = datetime.timedelta(days=1)
new_date = datetime_obj + delta
print(new_date.date())
```
This will output "2021-10-02", which is the next day after the original date object.
阅读全文