Python Timer Cooldown Example

Viewed 674

I'm looking for a cooldown timer for python, basically just to print days,hours,minutes,seconds left from a certain date.

Thanks very much!

5 Answers

Try this. The module datetime is preinstalled on Python, I believe.

import datetime

while True:
    print("\033[H\033[J")
    present = datetime.datetime.now()
    future = datetime.datetime(2022, 3, 31, 8, 0, 0)
    difference = future - present
    print(difference)

The format for datetime's future is: year, month, day, hour, minute, second.

Or, if you'd like to have user input:

import datetime

year = int(input('Enter the year of the end date: '))
month = int(input('Enter the month of the end date: '))
day = int(input('Enter the day of the end date: '))
hour = int(input('Enter the hour of the end date: '))
minute = int(input('Enter the minute of the end date: '))
second = int(input('Enter the second of the end date (a little tricky): '))
future = datetime.datetime(year, month, day, hour, minute, second)
while True:
    print("\033[H\033[J")
    present = datetime.datetime.now()
    difference = future - present
    if present >= future:
        break
    print(difference)

print('Time reached!')

You can get the counter with the help of time delta function.

import datetime
import time


future_date = datetime.datetime.now()+ datetime.timedelta(seconds=3)


while True:
    curr_date = datetime.datetime.now()
    rem_time = future_date - curr_date
    total_seconds = int(rem_time.total_seconds())

    if total_seconds > 0:
        days, h_remainder = divmod(total_seconds, 86400)
        hours, remainder = divmod(h_remainder, 3600)
        minutes, seconds = divmod(remainder, 60)
        print("Time Left: {} days, {} hours, {} minutes, {} seconds".format(days, hours, minutes, seconds))
        time.sleep(1)
    else:
        break

sample output will be:

Time Left: 0 days, 0 hours, 0 minutes, 2 seconds

Time Left: 0 days, 0 hours, 0 minutes, 1 seconds

You can use the seconds from a timedelta from subtracting two dates to calculate the days, hours, minutes and seconds like this:

from datetime import datetime
import time
totalSecs = 1 #So the while loop doesn't stop immidiately
while totalSecs > 0:
    startDate = datetime.now() #Can be any date
    endDate = datetime(2021, 12, 25)
    delta = endDate - startDate
    totalSecs = delta.total_seconds()
    days = divmod(totalSecs, 86400)
    hrs = divmod(days[1], 3600)
    mins = divmod(hrs[1], 60)
    seconds = divmod(mins[1], 1)
    print("{:02d}:{:02d}:{:02d}:{:02d}".format(int(days[0]), int(hrs[0]), int(mins[0]), int(seconds[0]))) #Zero pad all the numbers
    time.sleep(1) #Print every second.

Thank you all for your replies, i've done a mistake when i made the post. Is not from a date. Is a countdown in day,hours,minutes,seconds from a certain amount of seconds. Let's say i've got 31104000 seconds and i want to print how many days,hours,minutes,seconds left from that amount of seconds.

The code i've got now is a bit trivial and i can't print seconds in realtime.

def SecondToDHM(time):
if time < 60:
    return "%.2f %s" % (time, SECOND)

second = int(time % 60)
minute = int((time / 60) % 60)
hour = int((time / 60) / 60) % 24
day = int(int((time / 60) / 60) / 24)

text = ""

if day > 0:
    text += str(day) + DAY
    text += " "

if hour > 0:
    text += str(hour) + HOUR
    text += " "

if minute > 0:
    text += str(minute) + MINUTE
    text += " "
    
if second > 0:
    text += str(second) + SECOND

return text
import datetime

a = datetime.datetime.now()

"%s:%s.%s" % (a.minute, a.second, str(a.microsecond))
Related