-5

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'))
gfffh
  • 3
  • 3
  • 2
    I don't understand the question. From what I can tell, this is an assignment, but you've lost some details. It looks like it should say, *"Ask the user to enter the date ... **then print:** ..."*. Is that right? As well, what do you need help with exactly? It looks like `b = int(input(...))` is wrong, and you shouldn't be taking an input for `d`. Is that what you want to ask about? Please read [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341). See also [ask]. You can [edit] to clarify. – wjandrea Nov 18 '22 at 02:31
  • Also please start with the [tour] if you haven't already – wjandrea Nov 18 '22 at 02:36
  • You've posted the problem you're writing code for, and the code you wrote so far - but what is your question? "I've tried doing my homework, but failed, please do it for me?" isn't a valid question on SO. – Grismar Nov 18 '22 at 02:41
  • If the user has to enter a date in format YYYY-MM-DD, then obviously that cannot be converted to int. Also, `datetime(b, '%Y-%m-%d')` won't work, maybe you should parse the date or choose the [right tool](https://docs.python.org/3/library/datetime.html#examples-of-usage-datetime) from the datetime module. – Ignatius Reilly Nov 18 '22 at 03:00
  • 1
    Don't do `import datetime` and later `from datetime import datetime`, it's impossible to know to what `datetime` refers afterwards. If you want to avoid to write a long `datetime.datetime` you can do `import datetime as dt` and then just `dt.datetime`. Also, avoid using variables names like `a`, `b`, `bb`... – Ignatius Reilly Nov 18 '22 at 03:07

1 Answers1

0

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
ELinda
  • 2,658
  • 1
  • 10
  • 9
  • Thank you for help. This topic is not difficult but with some certain points. Yes, now l notice the problem exactly in format that 'input' requires, especially adding " date_time_str". I have some questions about weekdays, will find answers in materials doing more examples knowing the right one. Thank you!! – gfffh Nov 19 '22 at 07:38