python 将ISO日期增加5天

i5desfxk  于 2023-05-21  发布在  Python
关注(0)|答案(2)|浏览(166)

我使用的日期格式如下所示:

2023-04-25T16:00:00+00:00

我想在current_date中添加5天,并返回与上面相同的格式
我试过这个:

from datetime import datetime, timedelta

today = datetime.now()
iso_date = today.isoformat()
iso_date += timedelta(days=5)

其抛出:

TypeError: can only concatenate str(not "datetime.timedelta") to str
klr1opcd

klr1opcd1#

isoformat()的返回值是一个字符串。您的timedelta应该添加到Date对象,而不是字符串:

today = datetime.now()
today += timedelta(days=5)
iso_date = today.isoformat()
a64a0gku

a64a0gku2#

from datetime import datetime, timedelta

today = datetime.now()
future_date = today + timedelta(days=5)
iso_date = future_date.isoformat()

print(iso_date)

输出:

2023-05-24T12:28:17.392856

相关问题