Python Program to Print the Incremented Date if valid
In this article, we will write a python program to input a date and check whether it is a valid date or not. If it is valid, output the incremented date. Otherwise, print “Invalid date”.
Input : 1/2/2000 Output: 2/2/2000 Input : 29/2/2002 Output: Invalid date Input : 31/12/2015 Output: 1/1/2016
The first step is to check whether the entered date is valid or not. For this step, we need to first obtain the maximum possible day number for the month entered. Then we need to see whether the day lies between 1 and the obtained maximum day number and whether the month lies between 1 and 12. If both these conditions get satisfied, that means it is a valid date, and we need to increment it. To increment the date, we need to handle the following cases:
- If the entered date is the last day of the year.
- If the entered date is the last day of the month.
- If the entered date is not the last day of the month.
For the first case, increment the year and set both the day and the month to 1. For the second case, increment the month and set the day to 1. For the third case, just increment the day. If even one of the two conditions did not get satisfied, then it is an invalid date.
Below is the implementation.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course