This is a Python Program to check if a date is valid and print the incremented date if it is.
The program takes in a date and checks if it a valid date and prints the incremented date if it is.
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.
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)
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.
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.
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs
- Apply for Python Internship
- Check Python Books