Adding days to a date in Python

The Question :

424 people think this question is useful

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 =

Which returned this error:

name 'timedelta' is not defined

The Question Comments :
  • 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.
  • Search didn’t work? All of these code examples would have helped: There appear to be over 200 questions just like this one.
  • possible duplicate of add days to a date in Python using loops, ranges, and slicing
  • 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

The Answer 1

635 people think this answer is useful

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)

The Answer 2

135 people think this answer is useful

Import timedelta and date first.

from datetime import timedelta, date

And will return today’s datetime, may be you want

EndDate = + timedelta(days=10)

The Answer 3

24 people think this answer is useful

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)

The Answer 4

14 people think this answer is useful

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

from datetime import datetime
from dateutil.relativedelta import relativedelta

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


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

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

The Answer 5

14 people think this answer is useful

If you want add days to date now, you can use this code

from datetime import datetime
from datetime import timedelta

date_now_more_5_days = ( + timedelta(days=5) ).strftime('%Y-%m-%d')

The Answer 6

12 people think this answer is useful

I guess you are missing something like that:

from datetime import timedelta

The Answer 7

9 people think this answer is useful

Here is a function of getting from now + specified days

import datetime

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

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

    return anotherTime.strftime(dateFormat)


addDays = 3 #days
output_format = '%d-%m-%Y'
output = get_date(output_format, addDays)
print output

The Answer 8

7 people think this answer is useful

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 clearer.

date_1 = 
end_date = date_1 + TimeDelta(days=10)

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

The Answer 9

1 people think this answer is useful

This Might Help import timedelta is missing

from datetime import date, timedelta
date1 = date(2011, 10, 10)
date2 = date + timedelta(days=5)
print (date2)

The Answer 10

0 people think this answer is useful

using timedeltas you can do:

import datetime

print("today :",today)

# One day different .
print("one day :",five_day)
#output - 1 day , 00:00:00

# five day extend .

# five day extend .
#output - 
today : 2019-05-29
one day : 5 days, 0:00:00
fitfthday 2019-06-03

The Answer 11

0 people think this answer is useful

Generally you have’got an answer now but maybe my class I created will be also helpfull. For me it solves all my requirements I have ever had in my Pyhon projects.

class GetDate:
    def __init__(self, date, format="%Y-%m-%d"): = pytz.timezone("Europe/Warsaw")

        if isinstance(date, str):
            date = datetime.strptime(date, format) = date.astimezone(

    def time_delta_days(self, days):
        return + timedelta(days=days)

    def time_delta_hours(self, hours):
        return + timedelta(hours=hours)

    def time_delta_seconds(self, seconds):
        return + timedelta(seconds=seconds)

    def get_minimum_time(self):
        return datetime.combine(, time.min).astimezone(

    def get_maximum_time(self):
        return datetime.combine(, time.max).astimezone(

    def get_month_first_day(self):
        return datetime(,, 1).astimezone(

    def current(self):

    def get_month_last_day(self):
        lastDay = calendar.monthrange(,[1]
        date = datetime(,, lastDay)
        return datetime.combine(date, time.max).astimezone(

How to use it

  1. = pytz.timezone("Europe/Warsaw") – here you define Time Zone you want to use in project
  2. GetDate("2019-08-08").current() – this will convert your string date to time aware object with timezone you defined in pt 1. Default string format is format="%Y-%m-%d" but feel free to change it. (eg. GetDate("2019-08-08 08:45", format="%Y-%m-%d %H:%M").current())
  3. GetDate("2019-08-08").get_month_first_day() returns given date (string or object) month first day
  4. GetDate("2019-08-08").get_month_last_day() returns given date month last day
  5. GetDate("2019-08-08").minimum_time() returns given date day start
  6. GetDate("2019-08-08").maximum_time() returns given date day end
  7. GetDate("2019-08-08").time_delta_days({number_of_days}) returns given date + add {number of days} (you can also call: GetDate( for yesterday)
  8. GetDate("2019-08-08").time_delta_haours({number_of_hours}) similar to pt 7 but working on hours
  9. GetDate("2019-08-08").time_delta_seconds({number_of_seconds}) similar to pt 7 but working on seconds

The Answer 12

0 people think this answer is useful

Sometimes we need to use searching by from date & to date. If we use date__range then we need to add 1 day to to_date otherwise queryset will be empty.


from datetime import timedelta  

from_date  = parse_date(request.POST['from_date'])

to_date    = parse_date(request.POST['to_date']) + timedelta(days=1)

attendance_list = models.DailyAttendance.objects.filter(attdate__range = [from_date, to_date])

Add a Comment