Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'method.': 0.05; 'try:': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'be:': 0.09; 'inserted': 0.09; 'object;': 0.09; 'parsed': 0.09; 'subject:skip:a 10': 0.09; 'properly': 0.15; 'result.': 0.15; "%y'": 0.16; "'%d": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'sys.exit(0)': 0.16; '\xce\xbc\xce\xbf\xcf\x81\xcf\x86\xce\xae': 0.16; 'wrote:': 0.17; 'string,': 0.17; 'appropriate': 0.20; 'written': 0.20; 'import': 0.21; 'insert': 0.23; 'statement': 0.23; 'task': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; 'checking': 0.27; 'displays': 0.27; 'errors.': 0.27; "doesn't": 0.28; 'convert': 0.29; 'could': 0.32; 'date.': 0.33; 'to:addr :python-list': 0.33; 'entered': 0.34; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'hello,': 0.39; 'received:192.168': 0.40; 'first': 0.61; 'within': 0.64; 'skip:= 30': 0.64; 'date,': 0.65; '\xcf\x80\xcf\x81\xce\xad\xcf\x80\xce\xb5\xce\xb9': 0.65; '\xcf\x83\xcf\x84\xce\xb7\xce\xbd': 0.65; 'price': 0.66; 'header :Reply-To:1': 0.68; '8bit%:100': 0.70; 'reply-to:no real name:2**0': 0.72; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=XeZXOvF5 c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=_vX2mLoUpDQA:10 a=qvbgYuf3kYAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=doxkKeqih_sA:10 a=vQD6fdtjFNbh35ozsrEA:9 a=QEXdDO2ut3YA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 21 Feb 2013 20:14:13 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Checking for valid date input and convert appropriately References: <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> In-Reply-To: <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361477655 news.xs4all.nl 6945 [2001:888:2000:d::a6]:36600 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39452 On 2013-02-21 19:38, Ferrous Cranus wrote: > import datetime from datetime Should be: from datetime import datetime > > try: > datetime.strptime( date, '%d %m %Y' ) That parses the date and discards the result. > date = date.strptime( '%Y-%m-%d' ) 'date' is a string, it doesn't have a 'strptime' method. When you parsed the date you got a datetime object; _that_ has the 'strptime' method. > except ValueError: > print( "

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. >