Parsing data input in Django Date?

I am trying to get a date for an event from a user. The input is a simple text input in HTML format. My main problem is that I do not know how to parse the date.

If I try to pass the raw string, I get a TypeError, as expected.

Does Django have date parsing modules?

+6
python django datetime
source share
3 answers

If you are using django.forms, see DateField.input_formats . This argument allows you to specify multiple date formats. DateField tries to parse raw data according to these formats in order.

+7
source share

Django, so to speak, does not do Python. I seem to be wrong here, as the answer on uptimebox shows.

Suppose you parse this line: "Wed Apr 21 19:29:07 +0000 2010" (this is from the Twitter JSON API)

You have analyzed it as a datetime object as follows:

import datetime JSON_time = 'Wed Apr 21 19:29:07 +0000 2010' my_time = datetime.datetime.strptime(JSON_time, '%a %b %d %H:%M:%S +0000 %Y') print type(my_time) 

You will get this by confirming that it is a datetime object:

 <type 'datetime.datetime'> 

More information on strptime() can be found here .

+7
source share

(In 2017, you can now use django.utils.dateparse

0
source share

All Articles