![]() |
Checking for valid date input and convert appropriately
import datetime from datetime
try: datetime.strptime( date, '%d %m %Y' ) date = date.strptime( '%Y-%m-%d' ) except ValueError: print( "<h2><font color=red>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 notvalid somehow i need to catch the exception otherwise the cgi-script displays python errors. |
Re: Checking for valid date input and convert appropriately
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( "<h2><font color=red>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. > |
Re: Checking for valid date input and convert appropriately
Τη **μπτη, 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 inorder 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 ifstatemnt like i do with the other user defined varibles for validity. if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and (date and datetime.strptime(date, '%Y-%m-%d') ) ): Thanks. |
Re: Checking for valid date input and convert appropriately
Τη **μπτη, 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 inorder 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 ifstatemnt like i do with the other user defined varibles for validity. if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and (date and datetime.strptime(date, '%Y-%m-%d') ) ): Thanks. |
Re: Checking for valid date input and convert appropriately
On 2013-02-21 21:22, 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. Correction: the datetime object has the 'strftime' for formatting the date. > > 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. > The 'strptime' method parses the string and returns a datetime object, and you can then use that object's 'strftime' method to format it to the desired string. > 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. > It _doesn't_ crash the cgi-script. You're telling it to exit if an exception is raised. If you don't want the cgi-script to exit, don't call 'exit'. > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ): > > Thanks. > |
Re: Checking for valid date input and convert appropriately
On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus <nikos.gr33k@gmail.com>
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. Michael |
Re: Checking for valid date input and convert appropriately
Τη *αρασκευή, 22 Φεβρουαρίου 2013 12:03:59 π.μ. UTC+2, ο χρήστης Michael Ross *γραψε:
> On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus <nikos.gr33k@gmail.com> > > 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 likei 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 ? |
Re: Checking for valid date input and convert appropriately
Τη *αρασκευή, 22 Φεβρουαρίου 2013 12:03:59 π.μ. UTC+2, ο χρήστης Michael Ross *γραψε:
> On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus <nikos.gr33k@gmail.com> > > 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 likei 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 ? |
Re: Checking for valid date input and convert appropriately
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? |
Re: Checking for valid date input and convert appropriately
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? |
| All times are GMT. The time now is 07:19 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.