概述
I have a date "10/10/11(m-d-y)" and I want to add 5 days to it using a Python script. Please consider a general solution that works on the month ends also.
I am using following code:
import re
from datetime import datetime
StartDate = "10/10/11"
Date = datetime.strptime(StartDate, "%m/%d/%y")
print Date -> is printing '2011-10-10 00:00:00'
Now I want to add 5 days to this date. I used the following code:
EndDate = Date.today()+timedelta(days=10)
Which returned this error:
name 'timedelta' is not defined
解决方案
The previous answers are correct but it's generally a better practice to do:
import datetime
Then you'll have, using datetime.timedelta:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
最后
以上就是曾经过客为你收集整理的python编程如何插入时间,在Python中为日期添加天数的全部内容,希望文章能够帮你解决python编程如何插入时间,在Python中为日期添加天数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复