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?
Submitted by Bipin Kumar, on October 11, 2019

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

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 to solve this problem:

  1. Initially, we will include the datetime module by using the import function.
  2. Take the date in the form of the date, month, year.
  3. 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.
  4. 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.

import datetime 

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

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.