Brig
Brig

Reputation: 10321

GAE Howto get Date from form Request

I have a form where the user can select a date. I would like to store that as a date time.

# Throws error 'duedate must be datetime'
goal.duedate = self.request.get('duedate')

Obviously this doens't work because the form field is a string. What is the best way to convert form fields that are dates into GAE datetime objects.

Upvotes: 1

Views: 1979

Answers (2)

David Underhill
David Underhill

Reputation: 16233

Use the datetime class's strptime function.

For example, if your form submits the date like '2011-02-15' then you could do this:

import datetime
str_due_date = self.request.get('duedate')
goal.duedate = datetime.datetime.strptime(str_due_date, '%Y-%m-%d')

Upvotes: 4

Nick Johnson
Nick Johnson

Reputation: 101139

Use the strptime() method of the datetime.datetime class, passing in a format string that matches your date's formatting.

Upvotes: 2

Related Questions