Created
October 25, 2012 15:20
-
-
Save johnschimmel/3953302 to your computer and use it in GitHub Desktop.
Example of getting Date and Time from HTML5 form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "layout/main.html" %} | |
{% block body %} | |
<div class="row"> | |
<div class="span6"> | |
<form method="POST"> | |
<label for="date">Pick a date</label> | |
<input type="date" name="date" id="date"> | |
<br> | |
<label for="time">Hour</label> | |
<input type="time" id="time" name="time"> | |
<br> | |
<input type="submit" value="Add Date"> | |
</form> | |
</div> | |
</div> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route("/date", methods=['GET','POST']) | |
def date_example(): | |
if request.method=="POST": | |
# get date | |
form_date = request.form.get('date') # in format 2012-10-25 or in Python String formatting %Y-%m-%d | |
# get time | |
form_time = request.form.get('time') # in format 24 hour, eg 1:30PM = 13:30 | |
# create Python date from form_date and form_time. We use the python datetime string formmatting to describe how the date is built YYYY-MM-DD HH:MM | |
date = datetime.datetime.strptime(form_date + " " + form_time,"%Y-%m-%d %H:%M") | |
# create your database document | |
# this is an example model | |
# mydoc = models.Mydata() | |
# mydoc.date = date # save date to the 'date' field | |
# mydoc.save() | |
return "The submitted date / time is %s" % str(date) | |
else: | |
return render_template("date_example.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment