Search code examples
pythondatetimepython-3.xdate-formattingstrptime

Python: strptime() formatting


I'm using CPython. This is the code:

import datetime
userBirthday = print("Enter your birthday(DD/MM/YYYY): ")
compBirthday = datetime.datetime.strptime(userBirthday, "%d/%b/%Y")

I get this error message:

ValueError: time data '13/12/2001' does not match format '%d/%b%Y'

Solution

  • You wanted this instead:

    datetime.datetime.strptime(userBirthday, "%d/%m/%Y")
    

    Where m stands for "month".

    Also, checkout dateutil.parser.parse, which can recognise a wide variety of formats.