Checking for valid date input and convert appropriately
Michael Ross
gmx at ross.cx
Thu Feb 21 19:57:03 EST 2013
On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <nikos.gr33k at gmail.com>
wrote:
> Please i have been trying hours for this:
Don't do that: Spending hours on being stuck. Take a break. Call it a
night.
Brain needs time to unstick itself.
Besides:
>>> from datetime import date
>>> entry='31 03 2013'
>>> day, month, year = int(entry[:2]), int(entry[3:5]), int(entry[6:])
>>> mydate=date(year,month,day)
>>> mydate.strftime('%Y-%m-%d')
'2013-03-31'
> try:
> datetime.strptime(date, '%d m% %Y')
> date = date.strftime('%Y-%m-%d')
> except:
> print( "<h2>date not propetly entered." )
> sys.exit(0)
> ===========================
>
> the user enters 21 02 2013
>
> a) i just need to check if its in accepted format
> b) then truncate the given date to mysql format
>
> That's all i want to do and i cant make it work. Plese tell me how to
> write this.
>
> I have tried anything i can think of.
More information about the Python-list
mailing list