Archive

Archive for November 29, 2018

Python code: Date Validation

November 29, 2018 Leave a comment



Python: Date Validation

As in any application there is lots of functions and procedures helping the application, some of these functions are directly related to the menu bar other are there just to work on back ground to perform a task and return something that another function need to complete an action.

Here is a python code to ask the user to input a date and check if the date is in the format we want to be.

I am new in python so maybe there is a code or a function to perform this, so if any one can help in this.


#python code: Date Validation

def valid_date():

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

try:

d,m,y=(my_date.split(‘/’))

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

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

if len(y)==2: y=’20’+y

if int(d)>31: print(‘Day must be less then 31’), brake

if int(m)>12: print(‘Mounth must be less then 12’), brake

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

return my_date

except:

print(‘Enter a valid date.’)

return ‘false’

vd=valid_date()
if vd == ‘false’ : valid_date()
else:
print(‘OK’, vd)