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














































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



Description:
 This program is for checking date if it is valid and print incremented date.
I used Python datetime module for this program.
Algorithm:
 1. Import datetime module
 2.Check date using datetime method of datetime module.

   datetime.date(year,month,day)

This method returns date if there is no error.
3. Handle the exception using try-except block.
Print invalid if format is invalid.
4. Increment date using datetime.date() and datetime.timedelta() method.

datetime.date(year,month,day)+datetime.timedelta(day=1)

5.End

Program:

#Python Program to Check if a Date is Valid and Print the Incremented Date if it is
import datetime
class Date:

def __init__(self):
pass
#method for checking date
def date_cheker(self,date):
year,month,day=date.split(
'-')
day=int(day)
month=int(month)
year=int(year)
#Exception handling
try:
datetime.datetime(year,month,day)
print(
"Incremented date is:",datetime.date(year,month,day) + datetime.timedelta(days=1))
except ValueError:
print(
"Date Invalid")

#creating a object
d1=Date()
date=input(
"Enter date in yy-mm-dd format: ")
d1.date_cheker(date)

   

Output: Enter date in yy-mm-dd format: 2016-2-28
Incremented date is: 2016-2-29

Enter date in yy-mm-dd format: 2019-5-10
Incremented date is: 2019-5-11

More Articles of Akash Modanwal:

Name Views Likes
Python Program to Check if a Date is Valid and Print the Incremented Date if it is 348 26

Comments