0

I am doing a counter down to user input date. I am stuck, don't know how to print hours, mins, secs. For example user input is: 2019-03-10, so the script need to count down: 6 days 23h 59mins 20secs left, 23h 59mins 19secs left etc. Any suggestions how to do it?

My code:

import datetime
import time

current_date = datetime.date.today()
print('Today is: ' + str(current_date))


def getdate():
    year = int(input('Enter a year: '))
    month = int(input('Enter a month: '))
    day = int(input('Enter a day: '))
    date_user = datetime.date(year, month, day)
    print(date_user)

    if date_user < current_date:
        print('This is past bro, wake up!')
        exit()
    elif date_user > current_date:
        print((date_user - current_date))


getdate()
Ninsu
  • 3
  • 3

1 Answers1

0

This is an example.

Notes:

  • It uses datetime.datetime.now() to get time including hours, minutes and seconds
  • It uses datetime.datetime(...) instead of datetime.date(...), because the latter is not compareable with datetime.datetime.now() (datetime and date are not compareable, because the latter lacks specific timing information
  • It uses https://stackoverflow.com/a/539360/8575607 to get minutes and hours from timedelta
  • It uses print(..., end="\r") to not always create a new line, and print("\n...") to force a new line
import datetime
import time

current_date = datetime.datetime.now() # Now gives current minutes, seconds...
print('Today is: ' + str(current_date))


def getdate():
    year = int(input('Enter a year: '))
    month = int(input('Enter a month: '))
    day = int(input('Enter a day: '))
    date_user = datetime.datetime(year, month, day, 0, 0, 0) # Midnight
    return date_user

date_user = getdate()

while date_user > current_date:
    current_date = datetime.datetime.now()
    diff = date_user - current_date
    # Using https://stackoverflow.com/a/539360/8575607
    s = diff.seconds
    # hours
    hours = s // 3600 
    # remaining seconds
    s = s - (hours * 3600)
    # minutes
    minutes = s // 60
    # remaining seconds
    seconds = s - (minutes * 60)
    print("{}day(s) {}h {}min(s) {}sec(s) left".format(diff.days, hours, minutes, seconds), end='\r')
    time.sleep(1)

print('\nThis is past bro, wake up!')

I would suggest you to better specify your question the next time. Examples would be "How do I print out a time delta the following way?" or "How do I execute a line of code every second until a goal is reached?" You went for asking about the bigger picture, which often does not give you that useful answers ^^

2xB
  • 296
  • 1
  • 11
  • Thanks! You opened my mind, could not to solve this problem. Ok, next time I will give more details about my problem, but hopefully you were able to understand me. – Ninsu Mar 04 '19 at 18:38
  • @Ninsu Great! Might you then mark this answer as correct or specify your question? – 2xB Mar 04 '19 at 18:42
  • @Ninsu Thank you. As this probably is not the place for a talk, I would now go my way. Good luck with your project! – 2xB Mar 04 '19 at 18:56