Write a Python program to validate the Date of Birth.

A date is always should in a proper format of MM/dd/yyyy or dd/MM/yyyy. How to validate a date as input below is the python code to validate the birthday date or Date in the format of dd/MM/yy.

How to validate a Date of Birth in Python?

Let’s take a Date of Birth as input and check whether it is legitimate or not with the assistance of few conditions.

What is a valid Date of Birth?

Input: Enter the date of birth: 26/06/15

Output: The date of birth is valid..

Input: Enter the date of birth: 20000/8000

Output: The date of birth is Invalid..

Algorithm to validate the Date:

  •       Accept input from the user.
  •       Split the data into the day, month, year, and store it to a new variable.
  •       Use DateTime validation to check whether it is valid or not.
  •       Print whether it is valid or not

Python program to validate the date of birth.

import datetime

inputDate = input("Enter the date of birth : ")

day,month,year = inputDate.split('/')

isValidDate = True
try :
    datetime.datetime(int(year),int(month),int(day))
    datetime.datetime(int(day), int(month), int(year))
    datetime.datetime(int(year), int(month),int(day))
except ValueError :
    isValidDate = False

if(isValidDate) :
    print ("The date of birth is valid ..")
else :
    print ("The date of birth is not valid..")

Output: Printing whether the date of birth is valid or not.