PyProgram #2 – Validating start and end dates

Time for another n00b code in Python. My colleague was trying to validate start and end dates, where start date has to be earlier (or lesser) than end date. I always love coding for n00b stuffs like this, so I decided to have some fun.

Problem: Given start_date and end_date, validate the dates for start_date <= end_date and print a message whether date is accepted or rejected.


Logic: I tried to use simple if-else-statements for the job, so I hit with the following logic.

check year1 < year2:
yes –> oK
no –> check year1 == year2:
no –> Quit
yes –> check month1 < month2:
yes –> OK
no –> check month1 == month2:
no –> Quit
yes –> check day1 < day2:
yes –> OK
no –> check day1 == day2:
yes –> OK
no = Quit

The start_date and end_date are split into the corresponding day, month and year values held in the arrays dd1 and dd2.

First the years are compred – if year2 is greater than year1, then the flag becomes 1. Else, equality of years is checked, if no then flag becomes 0.

For equal year values, months are compared – if month2 is greater than month1, flag becomes 1. Else, equality of months is checked, if no then flag becomes o.

For equal year and month values, days are compared. If day2 is greater than day1 or if the dates are equal, then flag becomes 1. Else the flag becomes 0.

Finally, if flag is 1 then the dates are accepted and if its 0 the dates are rejected.


def checker(date1,date2):
if int(dd1[2])def checker(date1,date2):
flag = 0
if int(dd1[2]) < int(dd2[2]):
flag = 1
elif int(dd1[2]) == int(dd2[2]):
if int(dd1[1]) < int(dd2[1]):
flag = 1
elif int(dd1[1]) == int(dd2[1]):
if int(dd1[0]) < int(dd2[0]):
flag = 1
elif int(dd1[0]) == int(dd2[0]):
flag = 1
flag = 0
flag = 0
flag = 0
if flag == 1:
print 'Dates accepted'
print 'Dates rejected'

Test: Now we can check the checker() function, with various possible date values. Here is the result..

>>> checker(’29-12-2006′,’01-01-2007′)
Dates accepted
>>> checker(’20-11-2006′,’20-12-2007′)
Dates accepted
>>> checker(’20-12-2006′,’22-12-2007′)
Dates accepted
>>> checker(’20-11-2006′,’20-12-2006′)
Dates accepted
>>> checker(’20-12-2006′,’22-12-2006′)
Dates accepted
>>> checker(’24-12-2006′,’22-12-2006′)
Dates rejected
>>> checker(’24-12-2006′,’24-12-2006′)
Dates accepted
>>> checker(’24-12-2007′,’24-12-2006′)
Dates rejected

Yay! Done!

Note: This code is to just show how to code in a plain way, for the proposed logic. But, this is not the best way to code in Python. The better way is to make use of built-in modules like ‘datetime’ module, which will help in doing the same stuff in a couple of lines or code, and also thats the Pythonic way of doing things. What I intend is to just show the transformation of a logic into expected result, using a simple python code using nested if-elif-else condition. 


4 thoughts on “PyProgram #2 – Validating start and end dates

  1. ewww wordpress suks.. it doest convert special chars automatically…

    In [3]: a = datetime.datetime (2006, 12, 14)

    In [4]: b = datetime.datetime (2006, 12, 15)

    In [5]: a<b
    Out[5]: True

Leave a Reply

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

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

Google+ photo

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

Connecting to %s