In this tutorial, we will check if a date is valid or not using python. A date is called valid if it actually exists in the calendar. Our program will ask the user to enter the date at the beginning of the program. It will then check the validity of the date and print out the result on the console. For example, if the date is ‘01/02/2012’ it will print ‘Input date is valid’ and if the date is ‘31/02/2012’, it will print ‘Input date is not valid..’.
For checking the validity of the date, we will use one python module called ‘datetime’. This module doesn’t provide any dedicated method to check if a date is valid or not but we will use this module with a simple trick to find out if a date is valid or not. Before going into details, let me quickly introduce you to the datetime module :
Python datetime module is one of the most useful modules to work with simple and complex time. We can import this module to a python program by using the ‘import datetime’ statement at the beginning of the program.
This module provides a lot of different methods to work with time. For example, we can use this module to print the current time, add days to the current time, add hours to the current time, add minutes to the current time etc. ‘datetime’ module can work with ‘naive’ and ‘aware’ kind of date time objects. ‘aware’ objects can hold additional information with the date time value like daylight saving information etc. These objects are useful if we are dealing with data from different timezones. ‘naive’ objects don’t contain any such information. These objects are easy to understand and we can use them if timezone, daylight saving etc. information are not required.
The smallest year supported by the ‘datetime’ module is stored in the MINYEAR variable and the maximum supported year is stored in the MAXYEAR variable. The value of MINYEAR is 1 and MAXYEAR is 9999.
As I have explained above, it doesn’t provide any method to check the validity of a date. We will use its constructor to create one ‘datetime’ object using the user provided values. If the constructor fails, it will throw one error. We will assume that the input values are not valid in that case.
- Get the input from the user
- Input should be in the form of dd/mm/yy
- Extract the inputs in different variables. e.g. if the user input is 02/04/99, we will extract the numbers 02, 04, and 99 and store them in three different variables.
- Use the constructor of ‘datetime’ module to check if the date is valid or not.
- Print out the result.
import datetime inputDate = input("Enter the date in format 'dd/mm/yy' : ") day,month,year = inputDate.split('/') isValidDate = True try : datetime.datetime(int(year),int(month),int(day)) except ValueError : isValidDate = False if(isValidDate) : print ("Input date is valid ..") else : print ("Input date is not valid..")
You can also download this program from here.
- The above example is compatible with python3. First of all, we are getting the date from the user input as ‘dd/mm/yy’.
- Using the ‘split’ method, we are extracting the day, month and year values from the string.
- isValidDate flag is used to determine if the user provided date is valid or not. If its value is ‘True’, it is a valid date, else it is not.
- datetime.datetime() is the constructor we are using to create one ‘datetime’ variable by using the user provided values. If it fails, it will throw one ‘ValueError’. We are setting the value of ‘isValidDate’ flag to ‘False’ here.
- Finally, print out the result to the user based on the value of ‘isValidDate’ flag.
- Python print current date,time,hour,minute,increment each
- Python program to find out numbers in a list divisible by two numbers
- Python program to remove all occurrence of a value from a list
- Python program to replace single or multiple character,substring in a string
- Python isinstance() and issubclass() functions
- Python program to pad zeros to a string