在Python中添加1天到我的约会

tim*_*ate 4 python date

我有以下日期格式:

year/month/day
Run Code Online (Sandbox Code Playgroud)

在我的任务中,我必须在此日期仅添加1天.例如:

date = '2004/03/30'
function(date)
>'2004/03/31'
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

ale*_*cxe 26

您需要标准库中的datetime模块.通过加载日期字符串strptime(),用于timedelta添加一天,然后使用strftime()将日期转储回字符串:

>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'
Run Code Online (Sandbox Code Playgroud)