How to make datetime.now function update when put within a loop












-1














I have created a for Loop that repeats the piece of code 100 times, the problem is that the datetime import won't update within the loop.



Code below:



from datetime import datetime
now = datetime.now()

print("%02d-%02d-%04d") % (now.year, now.month, now.day)
#below is the loop
for i in range(0, 100):
time.sleep(1)
print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


all it outputs is the same hours, minutes and seconds as when I first pressed run.



Output below:

11:59:0711:59:07

11:59:0711:59:07


(This carries on for 100 times)










share|improve this question





























    -1














    I have created a for Loop that repeats the piece of code 100 times, the problem is that the datetime import won't update within the loop.



    Code below:



    from datetime import datetime
    now = datetime.now()

    print("%02d-%02d-%04d") % (now.year, now.month, now.day)
    #below is the loop
    for i in range(0, 100):
    time.sleep(1)
    print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


    all it outputs is the same hours, minutes and seconds as when I first pressed run.



    Output below:

    11:59:0711:59:07

    11:59:0711:59:07


    (This carries on for 100 times)










    share|improve this question



























      -1












      -1








      -1







      I have created a for Loop that repeats the piece of code 100 times, the problem is that the datetime import won't update within the loop.



      Code below:



      from datetime import datetime
      now = datetime.now()

      print("%02d-%02d-%04d") % (now.year, now.month, now.day)
      #below is the loop
      for i in range(0, 100):
      time.sleep(1)
      print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


      all it outputs is the same hours, minutes and seconds as when I first pressed run.



      Output below:

      11:59:0711:59:07

      11:59:0711:59:07


      (This carries on for 100 times)










      share|improve this question















      I have created a for Loop that repeats the piece of code 100 times, the problem is that the datetime import won't update within the loop.



      Code below:



      from datetime import datetime
      now = datetime.now()

      print("%02d-%02d-%04d") % (now.year, now.month, now.day)
      #below is the loop
      for i in range(0, 100):
      time.sleep(1)
      print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


      all it outputs is the same hours, minutes and seconds as when I first pressed run.



      Output below:

      11:59:0711:59:07

      11:59:0711:59:07


      (This carries on for 100 times)







      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 12:08









      Matthieu Brucher

      12.1k22139




      12.1k22139










      asked Nov 12 '18 at 12:07









      Jordan Maertens

      32




      32
























          3 Answers
          3






          active

          oldest

          votes


















          2














          You have to call now again:



          for i in range(0, 100):
          time.sleep(1)
          now = datetime.now()
          print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


          now is an object, it doesn't get the new time when you use it (it would be terrible).






          share|improve this answer























          • Great help thank you!
            – Jordan Maertens
            Nov 12 '18 at 12:09










          • @DanielRoseman ??? Why did you remove the () ??
            – Matthieu Brucher
            Nov 12 '18 at 12:11










          • you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
            – vencaslac
            Nov 12 '18 at 12:13










          • Yes, I know, someone made an edit to remove the call!
            – Matthieu Brucher
            Nov 12 '18 at 12:14










          • No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
            – Daniel Roseman
            Nov 12 '18 at 12:22





















          0














          you are only sampling now one time, you need to do it inside the for loop like so



          from datetime import datetime
          now = datetime.now()

          print("%02d-%02d-%04d") % (now.year, now.month, now.day)
          #below is the loop
          for i in range(0, 100):
          time.sleep(1)
          now = datetime.now()
          print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





          share|improve this answer





























            0














            Why don't you move the now = datetime.now() within the loop?



            for i in range(0,100):
            time.sleep(1)
            now = datetime.now()
            print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





            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',
              autoActivateHeartbeat: false,
              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%2f53261870%2fhow-to-make-datetime-now-function-update-when-put-within-a-loop%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You have to call now again:



              for i in range(0, 100):
              time.sleep(1)
              now = datetime.now()
              print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


              now is an object, it doesn't get the new time when you use it (it would be terrible).






              share|improve this answer























              • Great help thank you!
                – Jordan Maertens
                Nov 12 '18 at 12:09










              • @DanielRoseman ??? Why did you remove the () ??
                – Matthieu Brucher
                Nov 12 '18 at 12:11










              • you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
                – vencaslac
                Nov 12 '18 at 12:13










              • Yes, I know, someone made an edit to remove the call!
                – Matthieu Brucher
                Nov 12 '18 at 12:14










              • No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
                – Daniel Roseman
                Nov 12 '18 at 12:22


















              2














              You have to call now again:



              for i in range(0, 100):
              time.sleep(1)
              now = datetime.now()
              print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


              now is an object, it doesn't get the new time when you use it (it would be terrible).






              share|improve this answer























              • Great help thank you!
                – Jordan Maertens
                Nov 12 '18 at 12:09










              • @DanielRoseman ??? Why did you remove the () ??
                – Matthieu Brucher
                Nov 12 '18 at 12:11










              • you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
                – vencaslac
                Nov 12 '18 at 12:13










              • Yes, I know, someone made an edit to remove the call!
                – Matthieu Brucher
                Nov 12 '18 at 12:14










              • No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
                – Daniel Roseman
                Nov 12 '18 at 12:22
















              2












              2








              2






              You have to call now again:



              for i in range(0, 100):
              time.sleep(1)
              now = datetime.now()
              print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


              now is an object, it doesn't get the new time when you use it (it would be terrible).






              share|improve this answer














              You have to call now again:



              for i in range(0, 100):
              time.sleep(1)
              now = datetime.now()
              print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)


              now is an object, it doesn't get the new time when you use it (it would be terrible).







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 '18 at 12:09

























              answered Nov 12 '18 at 12:08









              Matthieu Brucher

              12.1k22139




              12.1k22139












              • Great help thank you!
                – Jordan Maertens
                Nov 12 '18 at 12:09










              • @DanielRoseman ??? Why did you remove the () ??
                – Matthieu Brucher
                Nov 12 '18 at 12:11










              • you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
                – vencaslac
                Nov 12 '18 at 12:13










              • Yes, I know, someone made an edit to remove the call!
                – Matthieu Brucher
                Nov 12 '18 at 12:14










              • No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
                – Daniel Roseman
                Nov 12 '18 at 12:22




















              • Great help thank you!
                – Jordan Maertens
                Nov 12 '18 at 12:09










              • @DanielRoseman ??? Why did you remove the () ??
                – Matthieu Brucher
                Nov 12 '18 at 12:11










              • you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
                – vencaslac
                Nov 12 '18 at 12:13










              • Yes, I know, someone made an edit to remove the call!
                – Matthieu Brucher
                Nov 12 '18 at 12:14










              • No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
                – Daniel Roseman
                Nov 12 '18 at 12:22


















              Great help thank you!
              – Jordan Maertens
              Nov 12 '18 at 12:09




              Great help thank you!
              – Jordan Maertens
              Nov 12 '18 at 12:09












              @DanielRoseman ??? Why did you remove the () ??
              – Matthieu Brucher
              Nov 12 '18 at 12:11




              @DanielRoseman ??? Why did you remove the () ??
              – Matthieu Brucher
              Nov 12 '18 at 12:11












              you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
              – vencaslac
              Nov 12 '18 at 12:13




              you need to make it now = datetime.now() otherwise you'll get an attribute error when you use now.hour
              – vencaslac
              Nov 12 '18 at 12:13












              Yes, I know, someone made an edit to remove the call!
              – Matthieu Brucher
              Nov 12 '18 at 12:14




              Yes, I know, someone made an edit to remove the call!
              – Matthieu Brucher
              Nov 12 '18 at 12:14












              No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
              – Daniel Roseman
              Nov 12 '18 at 12:22






              No, I made an edit to add them. You left them off originally, then subsequently overwrote my edit with a previous version which still didn't have them.
              – Daniel Roseman
              Nov 12 '18 at 12:22















              0














              you are only sampling now one time, you need to do it inside the for loop like so



              from datetime import datetime
              now = datetime.now()

              print("%02d-%02d-%04d") % (now.year, now.month, now.day)
              #below is the loop
              for i in range(0, 100):
              time.sleep(1)
              now = datetime.now()
              print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





              share|improve this answer


























                0














                you are only sampling now one time, you need to do it inside the for loop like so



                from datetime import datetime
                now = datetime.now()

                print("%02d-%02d-%04d") % (now.year, now.month, now.day)
                #below is the loop
                for i in range(0, 100):
                time.sleep(1)
                now = datetime.now()
                print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





                share|improve this answer
























                  0












                  0








                  0






                  you are only sampling now one time, you need to do it inside the for loop like so



                  from datetime import datetime
                  now = datetime.now()

                  print("%02d-%02d-%04d") % (now.year, now.month, now.day)
                  #below is the loop
                  for i in range(0, 100):
                  time.sleep(1)
                  now = datetime.now()
                  print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





                  share|improve this answer












                  you are only sampling now one time, you need to do it inside the for loop like so



                  from datetime import datetime
                  now = datetime.now()

                  print("%02d-%02d-%04d") % (now.year, now.month, now.day)
                  #below is the loop
                  for i in range(0, 100):
                  time.sleep(1)
                  now = datetime.now()
                  print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 12:09









                  vencaslac

                  1,002217




                  1,002217























                      0














                      Why don't you move the now = datetime.now() within the loop?



                      for i in range(0,100):
                      time.sleep(1)
                      now = datetime.now()
                      print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





                      share|improve this answer


























                        0














                        Why don't you move the now = datetime.now() within the loop?



                        for i in range(0,100):
                        time.sleep(1)
                        now = datetime.now()
                        print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





                        share|improve this answer
























                          0












                          0








                          0






                          Why don't you move the now = datetime.now() within the loop?



                          for i in range(0,100):
                          time.sleep(1)
                          now = datetime.now()
                          print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)





                          share|improve this answer












                          Why don't you move the now = datetime.now() within the loop?



                          for i in range(0,100):
                          time.sleep(1)
                          now = datetime.now()
                          print ("%02d:%02d:%02d") % (now.hour, now.minute, now.second)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 '18 at 12:09









                          Siddharth Audhinarayanan

                          11




                          11






























                              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%2f53261870%2fhow-to-make-datetime-now-function-update-when-put-within-a-loop%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

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues