-2

How can i countdown to even with loop

import datetime

Today = datetime.datetime.today()
NewYear = datetime.datetime(2021, 2, 12)
print(NewYear)
print(Today)
while Today != NewYear:
    print(NewYear - Today)
  • In the loop subtract or add one day from the start date. – wwii Jan 29 '20 at 17:23
  • Please format the code - select it and type `ctrl-k`. .. [Formatting help](https://stackoverflow.com/help/formatting) ... [more Formatting](https://stackoverflow.com/editing-help) ... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Jan 29 '20 at 17:25
  • 1
    Possible duplicate: [How to increment a datetime by one day?](https://stackoverflow.com/questions/3240458/how-to-increment-a-datetime-by-one-day) – wwii Jan 29 '20 at 17:29
  • 1
    Does this answer your question? [How to increment a datetime by one day?](https://stackoverflow.com/questions/3240458/how-to-increment-a-datetime-by-one-day) – NutCracker Jan 29 '20 at 17:36
  • when i run code, i dont really know why the mili second didnt count down ? but when i dont using loop and type run many time, the mili second still countdown – Phạm Tuấn Jan 29 '20 at 17:42
  • Variable and function names should follow the `lower_case_with_underscores` style. `import *` is generally discouraged. – AMC Jan 29 '20 at 17:54
  • @AMC i'm just beginner, thanks for guide me – Phạm Tuấn Jan 29 '20 at 18:01
  • It isn't clear what you want to do. The [`datetime`](https://docs.python.org/3/library/datetime.html) documentation has many examples of doing math with `datetime` objects and some table of supported operations. When doing math operations you eventually end up working with `datetime.timedelta` objects. Your loop never stops because you never change the value of `Today` - you never assign anything different to it. If you search with `python datetime math` there should be plenty to read. – wwii Jan 29 '20 at 23:29

1 Answers1

0
import datetime

today = datetime.datetime.today()
new_year = datetime.datetime(2021, 2, 12)
while today < new_year:
    today = datetime.datetime.today()
    print(new_year - today)

I really did it guy, thanks for helping :)