Python Program to Check if a Date is Valid and Print the Incremented Date if it is

This is a Python Program to check if a date is valid and print the incremented date if it is.

Problem Description

The program takes in a date and checks if it a valid date and prints the incremented date if it is.

Problem Solution

1. Take in the date of the form: dd/mm/yyyy.
2. Split the date and store the day, month and year in separate variables.
3. Use various if-statements to check if the day, month and year are valid.
4. Increment the date if the date is valid and print it
5. Exit.

Program/Source Code

Here is source code of the Python Program to check if a date is valid and print the incremented date if it is. The program output is also shown below.

 
date=input("Enter the date: ")
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)
Program Explanation

1. User must enter the date of the form dd/mm/yy.
2. The date is then split and the day, month and year is stored in separate variables.
3. If the day isn’t between 1 and 30 for the months of April, June, September and November, the date is invalid.
4. If the day isn’t between 1 and 31 for the months January, March, April, May, July, August, October and December, the date is invalid.
5. If the month is February, the day should be between 1 and 28 for years other than the leap year and between 1 and 29 for leap years.
6. If the date is valid, the date should be incremented.
7. The final result is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1
Enter the date: 5/7/2016
The incremented date is:  6 7 2016
 
Case 2
Enter the date: 30/2/1997
Date is invalid.
 
Case 3
Enter the date: 31/12/2016
The incremented date is:  1 1 2017

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.