## countdown date helpme pleeeez

goodcop911
Posts: 1
Joined: Sun May 10, 2020 9:06 pm

### countdown date helpme pleeeez

I'm a beginner coder in python I'm trying to loop my program that counts down to a specific date June 4th 2020
how do I loop this program:

from datetime import datetime

futuredate = datetime.strptime('Jun 4 2020 13:33', '%b %d %Y %H:%M')

nowdate = datetime.now()
count = int((futuredate-nowdate).total_seconds())

days = count//86400
hours = (count-days*86400)//3600
minutes = (count-days*86400-hours*3600)//60
seconds = count-days*86400-hours*3600-minutes*60
print("{} days {} hours {} minutes {} seconds left".format(days, hours, minutes, seconds))

lugee
Posts: 10
Joined: Sat May 16, 2020 10:41 am
Location: Stockholm, Sweden

### Re: countdown date helpme pleeeez

DougieLawson
Posts: 38553
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: countdown date helpme pleeeez

You can do maths on a datetime object to find the time difference.
You can turn a datatime difference into the number of seconds between now and the target date.
If you modulo divide that by 86400 the integer result is the number of days.
If you modulo divide that remainder by 3600 the integer result is the number of hours.
If you modulo divide that remainder by 60 the integer result is number of minutes, the remainder is number of seconds.

I've wrapped some TKinter controls round it to make it a nice friendly pop-up window.

Code: Select all

#!/usr/bin/python3
import tkinter as tk
import tkinter.messagebox
from datetime import datetime

class Application(tk.Frame):

end_date = datetime.strptime('Jun 4 2020 13:33', '%b %d %Y %H:%M')
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.build_interface()

def build_interface(self):
self.clock = tk.Label(self, text="00 00:00:00", font=("Courier", 20), width=13)
self.clock.grid(row=1, column=1, stick="S")

self.time_label = tk.Label(self, text="days hours mins secs", font=("Courier", 10), width=20)
self.time_label.grid(row=2, column=1, sticky="N")
self.timer()

def calculate(self):
self.days = divmod(self.seconds_left, 86400)
self.hours = divmod(self.days[1], 3600)
self.mins = divmod(self.hours[1], 60)
self.secs = int(self.mins[1])
return "{:02} {:02d}:{:02d}:{:02d}".format(int(self.days[0]), int(self.hours[0]), int(self.mins[0]), self.secs)

def timer(self):
self.now = datetime.now()
self.seconds_left = (self.end_date - self.now).total_seconds()
if self.seconds_left <= 0:
self.clock.configure(text="Time's up!")
else:
self.clock.configure(text=self.calculate())
self.after(1000, self.timer)

if __name__ == "__main__":
root = tk.Tk()
root.title("TIMER")
Application(root).pack(side="top", fill="both", expand=True)
root.mainloop()
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.