Home > Projects/Experiments, Python > Python: Date Validation Function

Python: Date Validation Function



Learning : Date Validation Function
Subject: Dll’s and Function

In late of 90’s, I start writing DLL files, Dll file or Dynamic Link Library is a file that contain instructions or function that can be used and reused with/by other applications. So if we have a function that we keep using it in most of our programs then we write it in a dll file and re-call it any time we want to.

Writing a function that can be added to a Dll file and will be used by all the team is not a simple as it appeared to be, Dll files often contains more than one functions so we may find ten or twenty functions in there most are related so a DLL file need to be a very well documented and each function has it’s own comments, variables, version number and summary of its task and what it will return back.

In this post we will write Python code for a date validation function, the function will take one argument and will return values as :
1. Function will return False and error message if the passed argument is not a valid date.
2. Function will return True and the date if the date is valid.

Date Validation Function:

# Date validation function
# Variables: This function will take one argument as a user input date.
# Returns: This dunction will return Fals and error_message each itme the user enter a not valid date.
# The functin will return True and the date in case it was correnct.
# The function will returns value as a list.

def valid_date(my_date):

# get the separator

the_separator = []

for each in my_date :

if not each.isdigit():

the_separator.append(each)

# If the user inter other that two separators then the date is invalid.

if len (the_separator) != 2 or (the_separator[0] != the_separator[1]):

error_message = “Date is not valid.”

return False, error_message

d,m,y = (my_date.split(the_separator[0]))

if not d.isdigit() or (int(d) > 31 or int(d) < 1 ):

error_message = ‘Day must be number and between (1-31).’

return False, error_message

if not m.isdigit() or (int(m) > 12 or int(m) < 1 ) :

error_message= ‘Mounth must be number and between (1-12).’

return False, error_message

if not y.isdigit() or len(y) != 4 or int(y) < 1:

error_message = ‘Year must be a 4-digit positive number. ‘

return False, error_message

# convert the days and month to two digits numbers

if len(d) == 1: d =’0′ + d

if len(m) == 1: m =’0′ + m

my_date = d + ‘/’ + m + ‘/’ + y

return True, my_date


So now if we want to call the function and pass the user input to it then examine the returns, we may use the While loop as here..


vd=[False,0]

while vd[0] == False :

my_date = input(‘\n Enter the date as dd/mm/yyyy :’)

vd = valid_date(my_date)

if not vd[0] : print(‘ ‘,vd[1])

print(“\n we have a valid date, it is .. “, vd[1])


… Have fun …



To Download my Python code (.py) files Click-Here





Follow me on Twitter..





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: