'datetime.date' object has no attribute 'timedelta'
时间: 2023-09-13 15:04:49 浏览: 260
c#中DateTime.Now函数的使用详解
5星 · 资源好评率100%
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.
阅读全文