Search code examples
pythonpython-3.xdateuser-input

Adding a user specified date to existing code


I'm a but stuck with being able to add a user defined date to this Days to Go code. Works well with a set date embedded. But can't get this to work with the input lines.

from datetime import datetime, time

b = input
event = (input('What is the name of your event?'))  # input the name of the event
year = int(input('Enter a year'))  # input the requires year
month = int(input('Enter a month'))  # input the required month
day = int(input('Enter a day'))  # input the required day

def date_diff_in_seconds(dt2, dt1):
    timedelta = dt2 - dt1
    return timedelta.days * 24 * 3600 + timedelta.seconds


def dhms_from_seconds(seconds):
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    return (days, hours, minutes, seconds)


# Specified date
date1 = datetime.date(b[1], b[2], b[3])

# Current date
date2 = datetime.now()

print("\n%d days, %d hours, %d minutes, %d seconds" %
      dhms_from_seconds(date_diff_in_seconds(date2, date1)))
print()

Solution

  • First, you wrongly used b=input. It means you want to use input function with function name b, such as event = b('what is the name of your event?').

    Instead, you can assign values to b like b = (event, year, month, day) after getting information using input().

    And you imported datetime module by from datetime import datetime you don't need to explitly say datetime.date, just date. However, you can use datetime rather than date here, as follows:

    from datetime import datetime, time
    
    #b = input -> wrong usage
    event = (input('What is the name of your event? '))  # input the name of the event
    year = int(input('Enter a year '))  # input the requires year
    month = int(input('Enter a month '))  # input the required month
    day = int(input('Enter a day '))  # input the required day
    b = (event, year, month, day) # you can assign date values to b
    
    def date_diff_in_seconds(dt2, dt1):
        timedelta = dt2 - dt1
        return timedelta.days * 24 * 3600 + timedelta.seconds
    
    
    def dhms_from_seconds(seconds):
        minutes, seconds = divmod(seconds, 60)
        hours, minutes = divmod(minutes, 60)
        days, hours = divmod(hours, 24)
        return (days, hours, minutes, seconds)
    
    
    # Specified date
    date1 = datetime(b[1], b[2], b[3]) # not datetime.date()
    
    # Current date
    date2 = datetime.now()
    
    print("\n%d days, %d hours, %d minutes, %d seconds" %
          dhms_from_seconds(date_diff_in_seconds(date2, date1)))
    print()
    
    # if you want to print the event together:
    print("\n%d days, %d hours, %d minutes, %d seconds left for %s" % (
                dhms_from_seconds(date_diff_in_seconds(date2, date1)) + (event,)))
    

    The result is like:

    What is the name of your event? birthday
    Enter a year 2022
    Enter a month 03
    Enter a day 19
    
    0 days, 14 hours, 40 minutes, 2 seconds
    0 days, 14 hours, 40 minutes, 2 seconds left for Sunday # in case that you print the event together