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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'except:': 0.07; 'try:': 0.07; 'subject:skip:a 10': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'itself.': 0.11; "'%d": 0.16; 'wrote:': 0.17; '>>>': 0.18; 'input': 0.18; 'feb': 0.19; 'trying': 0.21; 'import': 0.21; 'skip:= 20': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'seems': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; '+0100,': 0.29; 'fri,': 0.30; 'print': 0.32; 'month,': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'michael': 0.36; 'but': 0.36; 'method': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'your': 0.60; 'day,': 0.60; 'spending': 0.61; '8bit%:95': 0.61; 'hours': 0.66; 'applying': 0.69; '8bit%:100': 0.70; '8bit%:92': 0.70; '2013': 0.84; 'break.': 0.84 X-Received: by 10.49.84.167 with SMTP id a7mr20735qez.11.1361507662542; Thu, 21 Feb 2013 20:34:22 -0800 (PST) Newsgroups: comp.lang.python Date: Thu, 21 Feb 2013 20:34:22 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.68.75.81; posting-account=DYJQ-woAAACEPH85Au2BhUVfFTfSfVa4 References: <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> <5bac38bb-df2e-47b8-96dd-2b1c090959e4@googlegroups.com> <49de0696-b6f6-42ce-9272-79dac37c9605@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 94.68.75.81 MIME-Version: 1.0 Subject: Re: Checking for valid date input and convert appropriately From: Ferrous Cranus To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361507671 news.xs4all.nl 6855 [2001:888:2000:d::a6]:46019 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39518 =CE=A4=CE=B7 =CE=A0=CE=B1=CF=81=CE=B1=CF=83=CE=BA=CE=B5=CF=85=CE=AE, 22 =CE= =A6=CE=B5=CE=B2=CF=81=CE=BF=CF=85=CE=B1=CF=81=CE=AF=CE=BF=CF=85 2013 2:57:0= 3 =CF=80.=CE=BC. UTC+2, =CE=BF =CF=87=CF=81=CE=AE=CF=83=CF=84=CE=B7=CF=82 M= ichael Ross =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: > On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus =20 > wrote: > > Please i have been trying hours for this: > Don't do that: Spending hours on being stuck. Take a break. Call it a =20 >=20 > night. >=20 > Brain needs time to unstick itself. > Besides: > >>> from datetime import date > >>> entry=3D'31 03 2013' > >>> day, month, year =3D int(entry[:2]), int(entry[3:5]), int(entry[6:]) >=20 >=20 >=20 > >>> mydate=3Ddate(year,month,day) > >>> mydate.strftime('%Y-%m-%d') >=20 > '2013-03-31' Tis seems very nice solution but also we need to check the user input for v= alidity. What if the user entered: 29 15 2013 ? i tried to use your method applying some date validation check but that als= o failed for me: try: if( datetime.strptime(date, '%d m% %Y') ): day, month, year =3D int(date[:2]), int(date[3:5]), int(date[6:]) date =3D date(year, month, day) date =3D date.strftime('%Y-%m-%d') except: print "....." =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Isn't method strptime check the user input for validity?