How to check if a date is valid or not in python

Published by admin on

Python check if a date is valid

Python program to check if a date is valid :

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 :

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.

 

The Algorithm to use :

1. Get the input from the user

2. Input should be in the form of dd/mm/yy

3. 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.

4. Use the constructor of ‘datetime’ module to check if the date is valid or not.

5. Print out the result.

 

Python Program :

You can download this program from here.

python check if date is valid

 

The above example is compatible with python3.

1. First of all, we are getting the date from the user input as ‘dd/mm/yy’.

2. Using the ‘split’ method, we are extracting the day, month and year values from the string.

3. 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.

4. 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.

5. Finally, print out the result to the user based on the value of ‘isValidDate‘ flag.

 

Sample Example :

python check if date is valid

 

You might also like :

Categories: python

3 Comments

meno · March 17, 2018 at 3:25 am

just a quick comment, in passing… I have been accustomed to setting a ‘verify’ boolean variable (e.g., isValidDate) to False by default, and then only set it to True once I have confirmed the assertion has been made.
This is just my opinion.
Thank you for sharing your work.

Shalom Nyende · April 18, 2018 at 10:06 am

Perhaps I am a little late for the party. I use python’s dateutil module:
>>>from dateutil.parser import parse
>>>parse(’11-11-11′)
Returns a valid datetime object.
You can feed it dates separated by / or –

Aivar Paalberg · May 22, 2018 at 10:20 am

There is possibility to skip splitting the inputDate and instead of doing validity check with:

datetime.datetime(int(year),int(month),int(day))

one can parse time directly from string using strptime() method:

datetime.datetime.strptime(inputDate, ‘%d/%m/%y’)

More information in Python documentation: https://docs.python.org/3.6/library/datetime.html#datetime.datetime.strptime

Leave a Reply

Your email address will not be published. Required fields are marked *