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
Here is a function of getting from now + specified days
Usage:
The previous answers are correct but it's generally a better practice to do:
Then you'll have, using
datetime.timedelta
:I guess you are missing something like that:
Here is another method to add days on date using dateutil's relativedelta.
Output:
In order to have have a less verbose code, and avoid name conflicts between datetime and datetime.datetime, you should rename the classes with CamelCase names.
So you can do the following, which I think it is clear.
Also, there would be no name conflict if you want to
import datetime
later on.Import
timedelta
first.And
Date.today()
will return today's datetime, may be you want