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
share|improve this question
5  
General clue: if you get the error name 'timedelta' is not defined, that means that you haven't defined timedelta anywhere. Python is usually pretty informative about its error messages. – katrielalex Jul 29 '11 at 10:06
1  
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one. – S.Lott Jul 29 '11 at 10:08
1  
possible duplicate of add days to a date in Python using loops, ranges, and slicing – S.Lott Jul 29 '11 at 10:11
4  
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5 – FeifanZ Jan 7 '13 at 0:53
up vote 312 down vote accepted

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)
share|improve this answer
3  
datetime.datetime - why twice? – paulmorriss Aug 11 '14 at 9:11
21  
importing like "from datetime import datetime, timedelta" would add readibility to the code – Manel Clos Nov 12 '14 at 13:31
5  
@paulmorriss: You are calling the strptime method on the datetime class in the datetime module, so you need to specify datetime.datetime. – Graeme Perrow Jan 6 '15 at 22:43
    
If explicit is better, u should avoid to short'n it. But, agree with from datetime import datetime, timedelta, it's a bit clear and did remove duplicated words at all. – erm3nda Feb 29 '16 at 12:59
1  
Long tail legacy problem there. it "should" be from datetime import DateTime since classes are CamelCased, but datetime precedes PEP8. – Aaron McMillin Jun 26 '17 at 20:28

Import timedelta first.

from datetime import timedelta

And Date.today() will return today's datetime, may be you want

EndDate = Date + timedelta(days=10)
share|improve this answer
9  
datetime.date.today() instead of Date.today() – elsadek Aug 6 '14 at 15:19
    
@dan-klasson It doesn't work for me, date object don't have timedelta method. What Python version are you using? – DrTyrsa Jul 17 '17 at 6:32
    
@DrTyrsa My bad. Should be: from datetime import timedelta, date; date.today() + timedelta(days=10) – dan-klasson Jul 17 '17 at 7:07

I guess you are missing something like that:

from datetime import timedelta
share|improve this answer

Here is another method to add days on date using dateutil's relativedelta.

from datetime import datetime
from dateutil.relativedelta import relativedelta

print 'Today: ',datetime.now().strftime('%d/%m/%Y %H:%M:%S') 
date_after_month = datetime.now()+ relativedelta(days=5)
print 'After 5 Days:', date_after_month.strftime('%d/%m/%Y %H:%M:%S')

Output:

Today: 25/06/2015 15:56:09

After 5 Days: 30/06/2015 15:56:09

share|improve this answer

If you happen to already be using pandas, you can save a little space by not specifying the format:

import pandas as pd
startdate = "10/10/2011"
enddate = pd.to_datetime(startdate) + pd.DateOffset(days=5)
share|improve this answer

Here is a function of getting from now + specified days

import datetime

def get_date(dateFormat="%d-%m-%Y", addDays=0):

    timeNow = datetime.datetime.now()
    if (addDays!=0):
        anotherTime = timeNow + datetime.timedelta(days=addDays)
    else:
        anotherTime = timeNow

    return anotherTime.strftime(dateFormat)

Usage:

addDays = 3 #days
output_format = '%d-%m-%Y'
output = get_date(output_format, addDays)
print output
share|improve this answer
1  
Good code. But your IF to test the addDays in get_date is not necessary – ECC Nov 24 '14 at 15:09

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.

from datetime import datetime as DateTime, timedelta as TimeDelta

So you can do the following, which I think it is clear.

date_1 = DateTime.today() 
end_date = date_1 + TimeDelta(days=10)

Also, there would be no name conflict if you want to import datetime later on.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.