Skip to content

Instantly share code, notes, and snippets.

@johnschimmel
Created October 25, 2012 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnschimmel/3953302 to your computer and use it in GitHub Desktop.
Save johnschimmel/3953302 to your computer and use it in GitHub Desktop.
Example of getting Date and Time from HTML5 form
{% 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 %}
@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