I am building a kind of calender web app
I have set up the following form in HTML
<form action='/event' method='post'>
Year ("yyyy"): <input type='text' name='year' />
Month ("mm"): <input type='text' name='month' />
Day ("dd"): <input type='text' name='day' />
Hour ("hh"): <input type='text' name='hour' />
Description: <input type='text' name='info' />
<input type='submit' name='submit' value='Submit'/>
</form>
The input from the user is then submited in the a cherrypy server
I am wondering, is there a way to check if the date entered by the user is a valid date?
Obviously I could write a whole lot of if statements, but are there any built in function that can check this?
Thanks
Here is a solution using time.
The question assumes that the solution without libraries involves "a whole lot of if statements", but it does not:
You could try doing
that will eliminate somethings like months >12 , hours > 23, non-existent leapdays (month=2 has max of 28 on non leap years, 29 otherwise, other months have max of 30 or 31 days)(throws ValueError exception on error)
Also you could try to compare it with some sanity upper/lower bounds. ex.:
The relevant upper and lower sanity bounds depend on your needs.
edit: remember that this does not handle certain datetimes things which may not be valid for your application(min birthday, holidays, outside hours of operation, ect.)
You can try using datetime and handle the exceptions to decide valid/invalid date : Example : http://codepad.org/XRSYeIJJ
You can try using datetime and handle the exceptions to decide valid/invalid date:
gives
This is improvement of an answer by DhruvPathak and makes more sense as an edit but it was rejected as "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer."
Use
datetime
eg.