Checking for valid date input and convert appropriately

Ferrous Cranus nikos.gr33k at gmail.com
Fri Feb 22 06:01:22 EST 2013


Τη Παρασκευή, 22 Φεβρουαρίου 2013 8:20:20 π.μ. UTC+2, ο χρήστης rob.mar... at gmail.com έγραψε:
> The datetime function: strptime() DOES check the date for validity. So try something like:
> 
> 
> 
> from datetime import datetime
> 
> 
> 
> def get_date():
> 
>   while True:
> 
>     try:
> 
>       date_in = raw_input("Enter date (dd mm yyyy): ")
> 
>       date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
> 
>       return date_out
> 
>     except ValueError:
> 
>       print "Invalid date: {}, try again...".format(date_in)

I'am thinking if somehting like the follwoing work:

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



More information about the Python-list mailing list