Add 1 day to my date in Python [duplicate]

眉间皱痕 提交于 2020-08-18 07:33:14

问题


I have the following date format:

year/month/day

In my task, I have to add only 1 day to this date. For example:

date = '2004/03/30'
function(date)
>'2004/03/31'

How can I do this?


回答1:


You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:

>>> 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'


来源:https://stackoverflow.com/questions/37089765/add-1-day-to-my-date-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!