0

I'm am trying to validate a string prompted for the user. This string should be write in the format dd/mm/aaaa and be a valid date. I thought than create a lot of if to check is not be ellegant, so i research and picked-up the datetime way.

import datetime

dataInput = input("Insira sua data de nascimento no formato 'dd/mm/aaaa': ")

ehDataValida = False

def validaData(dataInput) :
    while True :
            try :
                if dataInput != datetime.datetime.strptime(dataInput, "%d/%m/%Y").strftime("%d/%m/%Y") :
                    raise ValueError
                return False
            except ValueError:
                print("erro!")
                dataInput = input("Insira sua data de nascimento no formato 'dd/mm/aaaa': ")
                return dataInput

validaData(dataInput)
print(validaData(dataInput))

It's not working and for each test I found a new bug. What i'm missing? After this, I will dataInput.split('/') and achieve the date in a listData=[dd, mm, yyyy]

Lucas Maraal
  • 39
  • 10
  • https://stackoverflow.com/questions/16870663/how-do-i-validate-a-date-string-format-in-python – cs95 Mar 23 '18 at 03:49
  • i saw this topic and many others but i can't figure out a solution. what I'm tryng to implement was found in this topic, but i not reach the behavior yet: just `check`, and in case of `error`, `prompt` the user again and, so , if validated, `update` the `data_input var` ... – Lucas Maraal Mar 23 '18 at 03:54
  • just remembered, reading ` / ` in won't work. – Vipin Mohan Mar 23 '18 at 04:06
  • try the modified answer @Lucas Maraal .. your input is being read as integer, eg: if you input 12/2/1992, it is performing division operation as ((12 / 2) / 1992) which results in zero – Vipin Mohan Mar 23 '18 at 04:10

1 Answers1

0

Your input is being read as integer, eg: if you input 12/2/1992, it is performing division operation as ((12 / 2) / 1992) which results in zero. Use raw_input instead

import datetime

dataInput = raw_input("Insira sua data de nascimento no formato 'dd/mm/aaaa': ")

ehDataValida = False

def validaData(dataInput) :
    try:
        datetime.datetime.strptime(dataInput, '%d/%m/%y')
    except ValueError:
        raise ValueError("Insira sua data de nascimento no formato 'dd/mm/aaaa': ")

validaData(dataInput)

Try this. It should work

while True:
    try:
        dataInput = raw_input("Insira sua data de nascimento no formato 'dd/mm/aaaa': ")
        date_formatted = datetime.datetime.strptime(dataInput, '%d/%m/%y')
        break
    except ValueError:
        raise ValueError("Insira sua data de nascimento no formato 'dd/mm/aaaa': ")
Vipin Mohan
  • 1,631
  • 1
  • 12
  • 22
  • could you be kind enough to translate 'Insira sua data de nascimento no formato 'dd/mm/aaaa': ' , I'm assuming it means wrong format , please try again, If you are trying to create an input prompt after exception error, No,, then my code is incomplete ,my code merely raises an error message. – Vipin Mohan Mar 23 '18 at 04:17
  • Yeah, this works. Just a thing: i think in Python do not have `raw_input`, but doing `dataInput = str(input("Insira sua data de nascimento no formato 'dd/mm/aaaa': "))` Works. Thank you. (the translate is: Insert you bithday's date in the format 'dd/mm/yyyy') if you want to see: https://ideone.com/XfU92V – Lucas Maraal Mar 23 '18 at 04:29
  • @LucasMaraal look at the edits, it should loop until it is successfull, – Vipin Mohan Mar 23 '18 at 04:31
  • i marked, but i tried this last edit solution and not woks... the another one works. – Lucas Maraal Mar 23 '18 at 05:28
  • let me check once more, – Vipin Mohan Mar 23 '18 at 05:47