Adding 5 days to a date in Python









up vote
261
down vote

favorite
24












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



















  • 8




    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.
    – Katriel
    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






  • 5




    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
261
down vote

favorite
24












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



















  • 8




    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.
    – Katriel
    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






  • 5




    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
261
down vote

favorite
24









up vote
261
down vote

favorite
24






24





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















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






python date datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 '16 at 12:09









gary

3,80022348




3,80022348










asked Jul 29 '11 at 9:17









MuraliKrishna

1,50131211




1,50131211







  • 8




    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.
    – Katriel
    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






  • 5




    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













  • 8




    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.
    – Katriel
    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






  • 5




    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








8




8




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.
– Katriel
Jul 29 '11 at 10:06





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.
– Katriel
Jul 29 '11 at 10:06





1




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




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




1




possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11




possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11




5




5




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





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













8 Answers
8






active

oldest

votes

















up vote
399
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


















  • 4




    datetime.datetime - why twice?
    – paulmorriss
    Aug 11 '14 at 9:11






  • 31




    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






  • 1




    Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
    – Xiong Chiamiov
    Jun 5 '17 at 17:47






  • 5




    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

















up vote
94
down vote













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
















  • 10




    datetime.date.today() instead of Date.today()
    – elsadek
    Aug 6 '14 at 15:19






  • 1




    @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

















up vote
12
down vote













I guess you are missing something like that:



from datetime import timedelta





share|improve this answer



























    up vote
    10
    down vote













    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



























      up vote
      10
      down vote













      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



























        up vote
        8
        down vote













        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

















        up vote
        4
        down vote













        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



























          up vote
          3
          down vote













          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 = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6871016%2fadding-5-days-to-a-date-in-python%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            399
            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


















            • 4




              datetime.datetime - why twice?
              – paulmorriss
              Aug 11 '14 at 9:11






            • 31




              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






            • 1




              Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
              – Xiong Chiamiov
              Jun 5 '17 at 17:47






            • 5




              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














            up vote
            399
            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


















            • 4




              datetime.datetime - why twice?
              – paulmorriss
              Aug 11 '14 at 9:11






            • 31




              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






            • 1




              Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
              – Xiong Chiamiov
              Jun 5 '17 at 17:47






            • 5




              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












            up vote
            399
            down vote



            accepted







            up vote
            399
            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














            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














            share|improve this answer



            share|improve this answer








            edited Sep 25 '15 at 9:43









            Mathieu Rodic

            4,59522939




            4,59522939










            answered Jul 29 '11 at 10:03









            Botond Béres

            9,53222539




            9,53222539







            • 4




              datetime.datetime - why twice?
              – paulmorriss
              Aug 11 '14 at 9:11






            • 31




              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






            • 1




              Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
              – Xiong Chiamiov
              Jun 5 '17 at 17:47






            • 5




              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












            • 4




              datetime.datetime - why twice?
              – paulmorriss
              Aug 11 '14 at 9:11






            • 31




              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






            • 1




              Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
              – Xiong Chiamiov
              Jun 5 '17 at 17:47






            • 5




              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







            4




            4




            datetime.datetime - why twice?
            – paulmorriss
            Aug 11 '14 at 9:11




            datetime.datetime - why twice?
            – paulmorriss
            Aug 11 '14 at 9:11




            31




            31




            importing like "from datetime import datetime, timedelta" would add readibility to the code
            – Manel Clos
            Nov 12 '14 at 13:31





            importing like "from datetime import datetime, timedelta" would add readibility to the code
            – Manel Clos
            Nov 12 '14 at 13:31





            5




            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




            @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




            1




            1




            Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
            – Xiong Chiamiov
            Jun 5 '17 at 17:47




            Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is datetime? You can't rely on convention to know, but always have to look at the imports.
            – Xiong Chiamiov
            Jun 5 '17 at 17:47




            5




            5




            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




            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












            up vote
            94
            down vote













            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
















            • 10




              datetime.date.today() instead of Date.today()
              – elsadek
              Aug 6 '14 at 15:19






            • 1




              @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














            up vote
            94
            down vote













            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
















            • 10




              datetime.date.today() instead of Date.today()
              – elsadek
              Aug 6 '14 at 15:19






            • 1




              @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












            up vote
            94
            down vote










            up vote
            94
            down vote









            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












            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












            share|improve this answer



            share|improve this answer










            answered Jul 29 '11 at 9:20









            DrTyrsa

            22.9k66379




            22.9k66379







            • 10




              datetime.date.today() instead of Date.today()
              – elsadek
              Aug 6 '14 at 15:19






            • 1




              @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












            • 10




              datetime.date.today() instead of Date.today()
              – elsadek
              Aug 6 '14 at 15:19






            • 1




              @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







            10




            10




            datetime.date.today() instead of Date.today()
            – elsadek
            Aug 6 '14 at 15:19




            datetime.date.today() instead of Date.today()
            – elsadek
            Aug 6 '14 at 15:19




            1




            1




            @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




            @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




            @DrTyrsa My bad. Should be: from datetime import timedelta, date; date.today() + timedelta(days=10)
            – dan-klasson
            Jul 17 '17 at 7:07










            up vote
            12
            down vote













            I guess you are missing something like that:



            from datetime import timedelta





            share|improve this answer
























              up vote
              12
              down vote













              I guess you are missing something like that:



              from datetime import timedelta





              share|improve this answer






















                up vote
                12
                down vote










                up vote
                12
                down vote









                I guess you are missing something like that:



                from datetime import timedelta





                share|improve this answer












                I guess you are missing something like that:



                from datetime import timedelta






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 29 '11 at 9:20









                vstm

                10.6k13742




                10.6k13742




















                    up vote
                    10
                    down vote













                    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
























                      up vote
                      10
                      down vote













                      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






















                        up vote
                        10
                        down vote










                        up vote
                        10
                        down vote









                        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












                        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












                        share|improve this answer



                        share|improve this answer










                        answered Aug 29 '14 at 14:10









                        fantabolous

                        7,60032836




                        7,60032836




















                            up vote
                            10
                            down vote













                            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
























                              up vote
                              10
                              down vote













                              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






















                                up vote
                                10
                                down vote










                                up vote
                                10
                                down vote









                                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












                                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












                                share|improve this answer



                                share|improve this answer










                                answered Jun 25 '15 at 12:56









                                Atul Arvind

                                9,82153449




                                9,82153449




















                                    up vote
                                    8
                                    down vote













                                    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














                                    up vote
                                    8
                                    down vote













                                    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












                                    up vote
                                    8
                                    down vote










                                    up vote
                                    8
                                    down vote









                                    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














                                    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














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 14 '14 at 8:17

























                                    answered Nov 14 '14 at 8:01









                                    Guray Celik

                                    76111011




                                    76111011







                                    • 1




                                      Good code. But your IF to test the addDays in get_date is not necessary
                                      – ECC
                                      Nov 24 '14 at 15:09












                                    • 1




                                      Good code. But your IF to test the addDays in get_date is not necessary
                                      – ECC
                                      Nov 24 '14 at 15:09







                                    1




                                    1




                                    Good code. But your IF to test the addDays in get_date is not necessary
                                    – ECC
                                    Nov 24 '14 at 15:09




                                    Good code. But your IF to test the addDays in get_date is not necessary
                                    – ECC
                                    Nov 24 '14 at 15:09










                                    up vote
                                    4
                                    down vote













                                    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
























                                      up vote
                                      4
                                      down vote













                                      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






















                                        up vote
                                        4
                                        down vote










                                        up vote
                                        4
                                        down vote









                                        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












                                        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












                                        share|improve this answer



                                        share|improve this answer










                                        answered Sep 27 '17 at 10:42









                                        toto_tico

                                        7,36234259




                                        7,36234259




















                                            up vote
                                            3
                                            down vote













                                            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 = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')





                                            share|improve this answer
























                                              up vote
                                              3
                                              down vote













                                              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 = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')





                                              share|improve this answer






















                                                up vote
                                                3
                                                down vote










                                                up vote
                                                3
                                                down vote









                                                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 = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')





                                                share|improve this answer












                                                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 = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Feb 20 at 19:51









                                                Jorge Omar Martinez

                                                14124




                                                14124



























                                                    draft saved

                                                    draft discarded
















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6871016%2fadding-5-days-to-a-date-in-python%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Use pre created SQLite database for Android project in kotlin

                                                    Darth Vader #20

                                                    Ondo