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

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

Given a date , the task is to Check if a Date is Valid and increment the given date and print it in python

Examples:

Example1:

Input:

given date ="11/02/2001"

Output:

The incremented given date is:  12 / 2 / 2001

Example2:

Input:

 given date = "29/02/2001"

Output:

The given date 29/02/2001 is not valid

Program to Check and Print the Incremented Date in Python

Below are the ways to check and implement the increment the given date in Python:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

1)By using if..elif..else Conditional Statements  and split() function(Static Input)

Approach:

  • Give the date in the format dd/mm/yyyy as static input
  • Separate the date by storing the day, month, and year in different variables.
  • Check the validity of the day, month, and year using various if-statements.
  • If the date is valid, increment it .
  • Print the increment date.
  • Exit of Program.

Below is the implementation:

# given given_data
given_date = "11/02/2001"
# splitting the given_data by / character to separate given_data,month and year
day, month, year = given_date.split('/')
day = int(day)
month = int(month)
year = int(year)
if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
    maxdays = 31
elif(month == 4 or month == 6 or month == 9 or month == 11):
    maxdays = 30
elif(year % 4 == 0 and year % 100 != 0 or year % 400 == 0):
    maxdays = 29
else:
    maxdays = 28
if(month < 1 or month > 12):
    print("The given date", given_date, "is not valid")
elif(day < 1 or day > maxdays):
    print("The given date", given_date, "is not valid")
elif(day == maxdays and month != 12):
    day = 1
    month = month+1
    print("The incremented given date is: ", day, month, year)
elif(day == 31 and month == 12):
    day = 1
    month = 1
    year = year+1
    print("The incremented given date is: ", day, '/', month, '/', year)
else:
    day = day + 1
    print("The incremented given date is: ",  day, '/', month, '/', year)

Output:

The incremented given date is:  12 / 2 / 2001

Explanation:

  • Give the date in the format dd/mm/yyyy as static input.
  • The date is then divided, with the day, month, and year recorded in separate variables.
  • The date is invalid if it is not between 1 and 30 in the months of April, June, September, and November.
  • The date is invalid if it is not between 1 and 31 for the months of January, March, April, May, July, August, October, and December.
  • If the month is February, the day should be between 1 and 28 for non-leap years and between 1 and 29 for leap years.
  • If the date is correct, it should be increased.
  • The final incremented date is printed

2)By using if..elif..else Conditional Statements  and split() function(User Input)

Approach:

  • Scan the date, month and year as int(input()).
  • Separate the date by storing the day, month, and year in different variables.
  • Check the validity of the day, month, and year using various if-statements.
  • If the date is valid, increment it .
  • Print the increment date.
  • Exit of Program.

Below is the implementation:

# Scan the date, month and year as int(input()).
day = int(input("Enter some random day = "))
month = int(input("Enter some random month = "))
year = int(input("Enter some random year = "))
if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
    maxdays = 31
elif(month == 4 or month == 6 or month == 9 or month == 11):
    maxdays = 30
elif(year % 4 == 0 and year % 100 != 0 or year % 400 == 0):
    maxdays = 29
else:
    maxdays = 28
if(month < 1 or month > 12):
    print("The given date", given_date, "is not valid")
elif(day < 1 or day > maxdays):
    print("The given date", given_date, "is not valid")
elif(day == maxdays and month != 12):
    day = 1
    month = month+1
    print("The incremented given date is: ", day, month, year)
elif(day == 31 and month == 12):
    day = 1
    month = 1
    year = year+1
    print("The incremented given date is: ", day, '/', month, '/', year)
else:
    day = day + 1
    print("The incremented given date is: ",  day, '/', month, '/', year)

Output:

Enter some random day = 11
Enter some random month = 2
Enter some random year = 2001
The incremented given date is: 12 / 2 / 2001

Related Programs: