Checking for valid date input and convert appropriately

Ferrous Cranus nikos.gr33k at gmail.com
Thu Feb 21 14:38:45 EST 2013


import datetime from datetime

try:
    datetime.strptime( date, '%d %m %Y' )
    date = date.strptime( '%Y-%m-%d' )
except ValueError:
    print( "<h2><font color=red>H ημερομηνία πρέπει να εισαχθεί στην σωστή μορφή => 21 05 2013!" )
    sys.exit(0)
================================

Hello, a have an html that among other thing require the user to insert a valid date.
The user must enter a date like "21 02 2013"

a) first i need to check if the entered date was inserted properly as above
b) convert the entered date to '%Y-%m-%d' which is an appropriate mysql date type format.

Also it would be nice if i could check the user entered date within an if statement like i check all my other fields instead of creating an extra ;try' statemnt just for checking tha date

=================================
if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ):
=================================

I want it to be written like the above, but if the user entered date is not valid somehow i need to catch the exception otherwise the cgi-script displays python errors.



More information about the Python-list mailing list