0

I am trying to add the following restrictions to the input date:

  • the format has to be ("%d/%m/%Y")
  • input date has to be one year < than the actual year

What I did:

   from datetime import datetime
   date1 = datetime.today().strftime("%d/%m/%Y")
   day1, month1, year1 = date1.split("/")
   BD = input("Enter your date of birth (format: DD/MM/YYYY): ")
   while BD != "%d/%m/%Y":          #<------------------------ does not stop if correct format    
     print("Error")
     BD = input("Enter your date of birth (format: DD/MM/YYYY): ")
   datetime.strptime(BD, "%d/%m/%Y")
   day, month, year = BD.split("/")
   while year1 < year:
     print("Birthday must be less than current year format has to be DD/MM/YYYY")
     BD = input("Enter your date of birth: ")
    

I have tried with .strftime(x, "%d/%m/%Y") but then it asked the input to be int and if I declared it, gave me an error because I guess it can not turn /'s to int. I tried so many things but I'm sure I'm missing some small thing! If anyone could help me with this, or just show me the simplest way, it would be cool! Thanks!

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    `BD != "%d/%m/%Y"` is true when BD does not *equal* this string, not when it is not in that *format*. – Scott Hunter Dec 07 '21 at 03:35
  • 1
    Check this out. https://www.kite.com/python/answers/how-to-validate-a-date-string-format-in-python – Mike Pinkman Dec 07 '21 at 03:58
  • "I have tried with .strftime(x, "%d/%m/%Y") but then it asked the input to be int" What does the documentation for this method tell you? When you read the documentation for `datetime`, do you see any related methods that might be useful? (Hint: do you know the words `format` and `parse`? Which one do you think describes the task you are trying to do? What do you think the `f` in `strftime` stands for?) – Karl Knechtel Dec 07 '21 at 04:05
  • Welcome to Stack Overflow. You are fundamentally asking two questions, and you are only supposed to ask one at a time. I tried, however, to give you duplicate links for everything related to your task. That said, you really should start with the [documentation](https://docs.python.org/3/library/datetime.html), or by [looking for](https://duckduckgo.com/?q=python+datetime+tutorial) a tutorial. – Karl Knechtel Dec 07 '21 at 04:08
  • Thanks for the comments and hints, I was mixing strptime and strftime. Next time I have to use a new command I will read about it first that's for sure! – Attila Antal Dec 08 '21 at 13:02

0 Answers0