Home > Problem, Projects/Experiments, Python > Python code: Date Validation

Python code: Date Validation




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)

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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