Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Countdown date to display correctly using Python 3

I'm trying to get a countdown that will display. Basically like a doomsday clock haha.

Might anyone be able to assist?

import os
import sys
import time
import datetime

def timer():
    endTime = datetime.datetime(2019, 3, 31, 8, 0, 0)

def countdown(count):
    while (count >= 0):
        print ('The count is: ', count)
        count -= 1
        time.sleep(1)

countdown(endTime)
print ("Good bye!")
like image 533
FDisME Avatar asked Oct 17 '25 10:10

FDisME


1 Answers

If you want to print out the countdown like a doomsday clock, you'll need to parse the timedelta value.

Is something like this what you are looking for?

import time
import datetime


def countdown(stop):
    while True:
        difference = stop - datetime.datetime.now()
        count_hours, rem = divmod(difference.seconds, 3600)
        count_minutes, count_seconds = divmod(rem, 60)
        if difference.days == 0 and count_hours == 0 and count_minutes == 0 and count_seconds == 0:
            print("Good bye!")
            break
        print('The count is: '
              + str(difference.days) + " day(s) "
              + str(count_hours) + " hour(s) "
              + str(count_minutes) + " minute(s) "
              + str(count_seconds) + " second(s) "
              )
        time.sleep(1)


end_time = datetime.datetime(2019, 3, 31, 19, 35, 0)
countdown(end_time)

# sample output
The count is: 44 day(s) 23 hour(s) 55 minute(s) 55 second(s) 
The count is: 44 day(s) 23 hour(s) 55 minute(s) 54 second(s) 
The count is: 44 day(s) 23 hour(s) 55 minute(s) 53 second(s) 
The count is: 44 day(s) 23 hour(s) 55 minute(s) 52 second(s) 
The count is: 44 day(s) 23 hour(s) 55 minute(s) 51 second(s) 
like image 84
DaveStSomeWhere Avatar answered Oct 20 '25 13:10

DaveStSomeWhere