Python Program to Check the Given Date is valid or not

Python program to check whether a date is valid or not; Through this tutorial, i am going to show you how to check whether a given date is valid or not in python program.

Python Program to Check the Given Date is valid or not

  • Write Python Program to Check the Given Date is valid or not

Write Python Program to Check the Given Date is valid or not

date=input("Enter the date in dd/mm/yy format: ")
dd,mm,yy=date.split('/')
dd=int(dd)
mm=int(mm)
yy=int(yy)
if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12):
    max1=31
elif(mm==4 or mm==6 or mm==9 or mm==11):
    max1=30
elif(yy%4==0 and yy%100!=0 or yy%400==0):
    max1=29
else:
    max1=28
if(mm<1 or mm>12):
    print("Date is invalid.")
elif(dd<1 or dd>max1):
    print("Date is invalid.")
elif(dd==max1 and mm!=12):
    dd=1
    mm=mm+1
    print("The incremented date is: ",dd,mm,yy)
elif(dd==31 and mm==12):
    dd=1
    mm=1
    yy=yy+1
    print("The incremented date is: ",dd,mm,yy)
else:
    dd=dd+1
    print("The incremented date is: ",dd,mm,yy)

Output

Enter the date in dd/mm/yy format:  25/04/2020 
The incremented date is:  26 4 2020

Recommended Python Tutorials

Leave a Comment