Home »
Python »
Python Programs
Python program to check the given Date is valid or not
Checking date is valid or not in Python: In this tutorial, we will learn how to check a given date is valid or not in the Python programming language?
By Bipin Kumar Last updated : January 04, 2024
Problem statement
Write a Python program to input a date, and check whether it is valid or not.
Checking the given date is valid or not
To check the given date is valid or not, we will use the datetime module in the program by using the import function and also we will use the try-except statement.
For example date 31-02-2020 is invalid because we know that February month has only 28 days in an ordinary year and a leap year it has 29 days but there the given date is 31 so, it is simply an invalid date. Here, we are going to do this work by using the Python programming language. Before going to solve this problem, we will learn the basic syntax of the try-except statement.
The datetime module
The datetime module is an inbuilt module in Python which provides us to solve various problems related to date and time.
The basic syntax of try-except statement:
try:
#statement
except:
#statement
- If the code or statement provided in the try block has no exception then only try executed.
- If any exception occurs in the block of try then try block skipped and except block will be executed.
Algorithm
The steps (algorithm) to check the given date is valid or not are:
- Initially, we will include the datetime module by using the import statement.
- Take the date in the form of the date, month, year.
- Since we know that, we going to check the date is valid or not and if the date is valid then ok but when it's invalid, we will ValueError. So, here we will use the try-except statement.
- If the try statement has no exception then we will print the given date is valid otherwise we will print the given date is invalid.
Let's see the implementation of the above algorithm in the Python program.
Python program to check the given date is valid or not
# Importing datetime module
import datetime
# Input the date as integers and mapping
# it to store the values to d, m, and y variables
d, m, y = map(int, input("Enter date: ").split())
try:
s = datetime.date(y, m, d)
print("Date is valid.")
except ValueError:
print("Date is invalid.")
Output
The output of the above example is:
RUN 1:
Enter date: 10 10 2010
Date is valid.
RUN2:
Enter date: 30 2 2019
Date is invalid.
Explanation
In the above program, we have imported the datetime module and taken the date in the form of d, m, y where d means day, m means month and y means year. Since some date input provided by the user may be valid or not that's why initially in the try block we are checking the date validation and if it is valid then print otherwise it will show ValueError. According to the try-except statement, the error found in try blocks is handled by except. So, we have except the ValueError and print the given date is invalid.
To understand the above program, you should have the basic knowledge of the following Python topics:
Python Basic Programs »