INTRODUCTION
This Python program validates user-inputted dates using the datetime module. It checks whether a given date is valid based on standard calendar rules, including:
Valid Month and Day Ranges: The program ensures that the month falls between 1 and 12 and that the day is appropriate for that month.
Leap Year Conditions: It accurately determines if February has 28 or 29 days based on the year.
Monthly Day Limits: The program confirms that months with only 30 days (April, June, September, November) do not exceed this limit, while it validates that months with 31 days (January, March, May, July, August, October, December) are also correct.
Additionally, this program features a user-friendly interface that allows users to input a year, month, and day, providing immediate feedback on the validity of the date.
PROGRAM
Full Program To Check The Given Date Is Valid Or Not
import datetime def checkdate(year,month,day): try: value=datetime.date(year,month,day) print("The Entered Date Is Valid:",value) except ValueError as e: if month<1 or month>12: print("INVAILD MONTH!!!:Month Should Be In 1 To 12") if day<1 or day>31: print("INVALID DAY!!!:Day Should Be In 1 To 31") if month==2: if year%4==0: if day > 29: print("INVALID!!!:February",year," Only Has 29 Days.") else: if day > 28: print("INVALID!!!:February",year,"Only Has 28 Days.") if month in [4,6,9,11] and day>30: print("INVALID!!!:Month",month,"Only Has 30 Days.") if year < 0: print("INVALID YEAR: Year Cannot Be Negative.") if year>9999: print("INVALID YEAR: Year Is Too Large.") while True: try: print("\t**********VALID OR INVALID DATE CHECK**********") print("\nSELECT THE BELOW OPTIONS") print("\v1.TO CHECK GIVEN DATE IS VALID OR INVALID") print("\v2.EXIT THE PROGRAM") options=int(input("ENTER THE OPTION[1/2]:")) if options==1: y=int(input("Enter The Year:")) m=int(input("Enter The Month:")) d=int(input("Enter The Day:")) checkdate(y,m,d) elif options==2: print("EXITING THE PROGRAM.....") break else: print("INVALID OPTIONS!!! PLEASE SELECT THE GIVEN OPTIONS[1/2]") except ValueError: print("INVALID INPUT!!! ENTER THE OPTIONS IN NUMBER[1/2]")
now lets understand in detail how the program works
Importing the datetime Module & checkdate Function
import datetime def checkdate(year,month,day):
First, the code imports the datetime module, which provides functions for manipulating dates and times. Next, the script defines a function called checkdate that takes three parameters: year, month, and day.
Try-Except Block
try: value = datetime.date(year, month, day) print("The Entered Date Is Valid:", value) except ValueError as e:
In this section, the code attempts to create a date object using the provided values. If successful, it prints a confirmation message. However, if the date is invalid, a ValueError occurs, and the code moves to the except block.
Month and Day Validation
if month < 1 or month > 12: print("INVALID MONTH!!!: Month Should Be In 1 To 12") if day < 1 or day > 31: print("INVALID DAY!!!: Day Should Be In 1 To 31")
Here, the code checks that the month falls between 1 and 12 and that the day is between 1 and 31. If either condition fails, the code prints an appropriate error message.
Leap Year Check for February
if month == 2: if year % 4 == 0: if day > 29: print("INVALID!!!: February", year, "Only Has 29 Days.") else: if day > 28: print("INVALID!!!: February", year, "Only Has 28 Days.")
This part specifically checks if the month is February (month 2). It first verifies whether the year is a leap year. If so, the day must not exceed 29 otherwise, it must not exceed 28.
30-Day Month Check
if month in [4, 6, 9, 11] and day > 30: print("INVALID!!!: Month", month, "Only Has 30 Days.")
In this section, the code confirms that if the month is one of those with only 30 days (April, June, September, November), the day must not exceed 30. If it does, an error message is printed.
Year Validity Checks
if year < 0: print("INVALID YEAR: Year Cannot Be Negative.") if year > 9999: print("INVALID YEAR: Year Is Too Large.")
Here, the code checks that the year is non-negative and does not exceed 9999. If either condition fails, it prints the relevant error message.
Main Loop for User Interaction
while True: try: print("\t********** VALID OR INVALID DATE CHECK **********") print("\nSELECT THE BELOW OPTIONS") print("\v1. TO CHECK IF GIVEN DATE IS VALID OR INVALID") print("\v2. EXIT THE PROGRAM") options = int(input("ENTER THE OPTION [1/2]: "))
This part begins an infinite loop where the user can choose to check a date or exit the program. It displays a menu with options.
Handling User Input
if options == 1: y = int(input("Enter The Year: ")) m = int(input("Enter The Month: ")) d = int(input("Enter The Day: ")) checkdate(y, m, d) elif options == 2: print("EXITING THE PROGRAM.....") break else: print("INVALID OPTION!!! PLEASE SELECT THE GIVEN OPTIONS [1/2]")
If the user selects option 1, the program prompts for a year, month, and day, then calls the checkdate function with these values. If the user selects option 2, the program exits. Otherwise, it prints an error message for invalid options.
Error Handling for Input
except ValueError: print("INVALID INPUT!!! ENTER THE OPTIONS IN NUMBER [1/2]")
Finally, if the user inputs a non-integer value, this except block catches the error and prompts the user for valid input.
OUTPUT
1. Valid Date
**********VALID OR INVALID DATE CHECK********** SELECT THE BELOW OPTIONS 1.TO CHECK GIVEN DATE IS VALID OR INVALID 2.EXIT THE PROGRAM ENTER THE OPTION[1/2]:1 Enter The Year:2005 Enter The Month:03 Enter The Day:17 The Entered Date Is Valid: 2005-03-17 **********VALID OR INVALID DATE CHECK********** SELECT THE BELOW OPTIONS 1.TO CHECK GIVEN DATE IS VALID OR INVALID 2.EXIT THE PROGRAM ENTER THE OPTION[1/2]:2 EXITING THE PROGRAM.....
2. Invalid Date
**********VALID OR INVALID DATE CHECK********** SELECT THE BELOW OPTIONS 1.TO CHECK GIVEN DATE IS VALID OR INVALID 2.EXIT THE PROGRAM ENTER THE OPTION[1/2]:1 Enter The Year:2005 Enter The Month:02 Enter The Day:30 INVALID!!!:February 2005 Only Has 28 Days. **********VALID OR INVALID DATE CHECK********** SELECT THE BELOW OPTIONS 1.TO CHECK GIVEN DATE IS VALID OR INVALID 2.EXIT THE PROGRAM ENTER THE OPTION[1/2]:2 EXITING THE PROGRAM.....
CONCLUSION
In conclusion, this Python program effectively validates dates by checking user inputs against established calendar rules. By ensuring valid month and day ranges, accounting for leap years, and enforcing monthly day limits, the program provides accurate feedback. Additionally, its user-friendly interface simplifies the process, allowing users to easily input dates and receive immediate validation results. Overall, this code serves as a reliable resource for anyone looking to understand and implement date handling in programming.