Checking for valid date input and convert appropriately

Tim Chase python.list at tim.thechases.com
Fri Feb 22 11:18:45 EST 2013


On 2013-02-22 07:07, Ferrous Cranus wrote:
> Actually it can, but instead of try: i have to create a function:
> 
> def is_sane_date(date):
>     parts = [int(part) for part in date.split() if part.isdigit()]
>     if len(parts) == 3 and \
>          1 <= parts[0] <= 31 and \
>          1 <= parts[1] <= 12 and \
>          1900 <= parts[2] <= 2100:
>         return True
>     return False

Then you have things like "31 2 2013" being a valid date.  Also, the
hard caps at 1900 and 2100 are both pretty short-sighted.  To the
best of my knowledge, there are valid dates outside that date
range :-)

-tkc





More information about the Python-list mailing list