Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Checking for valid date input and convert appropriately

Reply
Thread Tools

Checking for valid date input and convert appropriately

 
 
Mark Lawrence
Guest
Posts: n/a
 
      02-21-2013
On 21/02/2013 23:18, Ferrous Cranus wrote:
> Then i try to save the date as MySQL wants but it just aint happening:
>
> date = datetime.strftime(date, '%Y-%m-%d')
>
> Can you help me please save the user entered date to MySQL format?
>


I suggest testing your code at the interactive prompt, so something like.

c:\Users\Mark>python
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:4 [MSC v.1600 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> date = '21 02 2013'
>>> date = datetime.strftime(date, '%Y-%m-%d')

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'strftime' requires a 'datetime.date' object but
received a 'str'

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
 
 
 
Michael Ross
Guest
Posts: n/a
 
      02-21-2013
On Fri, 22 Feb 2013 00:08:01 +0100, Ferrous Cranus <>
wrote:

> Τη *αρασκευή, 22 Φεβρουαρίου 2013 12:03:59 π.μ. UTC+2, ο χρήστης Michael
> Ross *γραψε:
>> On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus
>> <>
>>
>> wrote:
>>
>>
>>
>> > Τη **μπτη, 21 Φεβρουαρίου 2013 10:14:13 μ..μ. UTC+2, ο χρήστης MRAB

>>
>> > *γραψε:

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

>>
>> >

>>
>> > I don't need to store the date i just want to make sure is entered

>>
>> > correctly.

>>
>> > I would like to have the user type the date like

>>
>> >

>>
>> > 21 02 2013

>>
>> > and then convert it to 2013-02-21 because thats how mysql wants the

>> date
>>
>> > in order to store it.

>>
>> > Please show me how to write this.

>>
>> >

>>
>> > Also, can you show me how to write it that if so even if the user

>>
>> > entered date is wrong it doesn't just crash the cgi-script?i know i

>> can
>>
>> > use tr: expect: but i want to avoid it, somehow i need to check the

>> date
>>
>> > in the same if statemnt like i do with the other user defined varibles

>>
>> > for validity.

>>
>> >

>>
>>
>>
>> You *have* to try/expect in order to not have the script crash.
>>
>> Think user typo: 21 02 2ß13
>>
>>
>>
>> Personally, I'd use a javascript on the html so users can't POST invalid
>>
>> dates.
>>
>> I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.13
>> as
>>
>> input,
>>
>> and you end up with 21.02.2013 posted to your cgi in any case.

>
> i just want to check for date validity from within ha same if statemnt
> like i check the other variables and i tried a regualr expression just
> now:
>
> if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and
> ( date and re.search( r'(\d+) (\d+) (\d+)', date ) ) ):
>
> Do i ahve somehting wrong in it?
>
> will the if become true if the user enters 21 02 2013 ?



I pretend not to know, try it out?

And try with invalid inputs, too:
211 02 2013
21 99 2013
31 02 2013
 
Reply With Quote
 
 
 
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Please i have been trying hours for this:

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.
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Please i have been trying hours for this:

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.
 
Reply With Quote
 
Michael Ross
Guest
Posts: n/a
 
      02-22-2013
On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <>
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.

 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 2:57:03 π.μ. UTC+2, ο χρήστης Michael Ross *γραψε:
> On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <>
> 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?
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 2:57:03 π.μ. UTC+2, ο χρήστης Michael Ross *γραψε:
> On Fri, 22 Feb 2013 01:12:40 +0100, Ferrous Cranus <>
> 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?
 
Reply With Quote
 
rob.marshall17@gmail.com
Guest
Posts: n/a
 
      02-22-2013
The datetime function: strptime() DOES check the date for validity. So try something like:

from datetime import datetime

def get_date():
while True:
try:
date_in = raw_input("Enter date (dd mm yyyy): ")
date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
return date_out
except ValueError:
print "Invalid date: {}, try again...".format(date_in)
 
Reply With Quote
 
rob.marshall17@gmail.com
Guest
Posts: n/a
 
      02-22-2013
The datetime function: strptime() DOES check the date for validity. So try something like:

from datetime import datetime

def get_date():
while True:
try:
date_in = raw_input("Enter date (dd mm yyyy): ")
date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
return date_out
except ValueError:
print "Invalid date: {}, try again...".format(date_in)
 
Reply With Quote
 
Ferrous Cranus
Guest
Posts: n/a
 
      02-22-2013
Τη *αρασκευή, 22 Φεβρουαρίου 2013 8:20:20 π.μ. UTC+2, ο χρήστης rob.mar...@gmail.com *γραψε:
> The datetime function: strptime() DOES check the date for validity. So try something like:
>
>
>
> from datetime import datetime
>
>
>
> def get_date():
>
> while True:
>
> try:
>
> date_in = raw_input("Enter date (dd mm yyyy): ")
>
> date_out = datetime.strptime(date_in,"%d %m %Y").strftime("%Y-%m-%d")
>
> return date_out
>
> except ValueError:
>
> print "Invalid date: {}, try again...".format(date_in)


Thank you very very much!! i cannot beleive that it was so easy, a matter of one line of coding!

date = datetime.strptime(date,"%d %m %Y").strftime("%Y-%m-%d")

Cna you please explain in to me?
This line checks the date variable for valid pattern entry and then also tranforms the date to the othjer pattern?

And if there is a way to embed this line to the existing if() statemtn along with the othwr variables check that would be perfect!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get DIV height in IE6 to set appropriately (IE7 works fine) Richard.Gohs@pa-tech.com HTML 1 09-24-2008 08:14 AM
How do I tell "imconplete input" from "valid input"? $B$?$+(B Python 2 05-29-2008 10:32 AM
Using size_t clearly (appropriately?) Mark Odell C Programming 40 07-13-2006 06:19 AM
Return appropriately by value, (smart) pointer, or reference Neal Coombes C++ 5 09-22-2005 10:22 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57