pythondays

could anyone identify the days python?


ask the user enter the date in the format YYYY-MM-DD

1.what age of user in days;

2.what day of week (in German language) was the birth.

import datetime

b = int(input('Enter your birthdate: '))
bb = datetime(b, '%Y-%m-%d')
a = datetime.date.today()
c = a-bb
print(c)


from datetime import datetime
d = input("Enter the date of birth: ")
print(d.strftime('%A'))

Solution

  • Your problem is trying to convert an input that's probably in YYYY-MM-DD format, into an int. This will not work in Python. Simply leave as a string and convert to a date.

    Use setlocale to choose German for output.

    from datetime import datetime
    from datetime import date
    
    # set language output to German
    import locale
    locale.setlocale(locale.LC_TIME, 'de_DE.UTF-8')
    
    # convert a str to a date, subtract with current date to get # of days 
    date_time_str = input('Enter your birthdate: ')
    bday = datetime.strptime(date_time_str, '%Y-%m-%d').date()
    today = date.today()
    print(today - bday)
    
    # reuse the "bday" variable defined above, get day of week (in German)
    print(bday.strftime('%A'))
    

    Output:

    730 days, 0:00:00
    Mittwoch