Checking for valid date input and convert appropriately
Ferrous Cranus
nikos.gr33k at gmail.com
Thu Feb 21 23:34:22 EST 2013
Τη Παρασκευή, 22 Φεβρουαρίου 2013 2:57:03 π.μ. UTC+2, ο χρήστης Michael Ross έγραψε:
> 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'
Tis seems very nice solution but also we need to check the user input for validity. What if the user entered: 29 15 2013 ?
i tried to use your method applying some date validation check but that also failed for me:
try:
if( datetime.strptime(date, '%d m% %Y') ):
day, month, year = int(date[:2]), int(date[3:5]), int(date[6:])
date = date(year, month, day)
date = date.strftime('%Y-%m-%d')
except:
print "....."
========================
Isn't method strptime check the user input for validity?
More information about the Python-list
mailing list