How to make datetime.now function update when put within a loop
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
add a comment |
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
add a comment |
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
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
python python-3.x
edited Nov 12 '18 at 12:08
Matthieu Brucher
12.1k22139
12.1k22139
asked Nov 12 '18 at 12:07
Jordan Maertens
32
32
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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).
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 itnow = datetime.now()
otherwise you'll get an attribute error when you usenow.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
add a comment |
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)
add a comment |
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)
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
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 itnow = datetime.now()
otherwise you'll get an attribute error when you usenow.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
add a comment |
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).
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 itnow = datetime.now()
otherwise you'll get an attribute error when you usenow.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
add a comment |
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).
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).
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 itnow = datetime.now()
otherwise you'll get an attribute error when you usenow.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
add a comment |
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 itnow = datetime.now()
otherwise you'll get an attribute error when you usenow.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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 12 '18 at 12:09
vencaslac
1,002217
1,002217
add a comment |
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 12 '18 at 12:09
Siddharth Audhinarayanan
11
11
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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