Problem with DateField input_formats

1,672 views
Skip to first unread message

mail....@gmail.com

unread,
Jun 23, 2010, 11:34:15 AM6/23/10
to Django users
I have:

DATE_INPUT_FORMATS = (
'%n/%j/%Y', '%n/%j/%y', # '10/25/2006', '10/25/06'
'%n-%j-%Y', '%n-%j-%y', # '10-25-2006', '10-25-06'
'%M %j %Y', '%M %j, %Y', # 'Oct 25 2006', 'Oct 25, 2006'
'%b %j %Y', '%b %j, %Y', # 'oct 25 2006', 'oct 25, 2006'
'%F %j %Y', '%F %j, %Y', # 'October 25 2006', 'October 25, 2006'
)


class CandidateProfileForm(forms.ModelForm):
class Meta:
model = Person
dob = forms.DateField(input_formats=DATE_INPUT_FORMATS)


But I'm not getting my date format through. For example, 4-20-1990
fails, even though I think it should be covered by %n-%j-%Y

I've also tried:
class CandidateProfileForm(forms.ModelForm):
class Meta:
model = Person
widgets = {
'dob':
forms.DateField(input_formats=DATE_INPUT_FORMATS),
}

with no luck. What am I missing here?

Karen Tracey

unread,
Jun 23, 2010, 11:54:35 AM6/23/10
to django...@googlegroups.com
On Wed, Jun 23, 2010 at 11:34 AM, bax...@gretschpages.com <mail....@gmail.com> wrote:
I have:

DATE_INPUT_FORMATS = (
   '%n/%j/%Y', '%n/%j/%y',  # '10/25/2006', '10/25/06'
   '%n-%j-%Y', '%n-%j-%y',  # '10-25-2006', '10-25-06'
   '%M %j %Y', '%M %j, %Y', # 'Oct 25 2006', 'Oct 25, 2006'
   '%b %j %Y', '%b %j, %Y', # 'oct 25 2006', 'oct 25, 2006'
   '%F %j %Y', '%F %j, %Y', # 'October 25 2006', 'October 25, 2006'
)


Looks like you are using the table from http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now, which is used for formatting date/times for output. The input formats, as noted http://docs.djangoproject.com/en/dev/ref/settings/#date-input-formats use Python's formatting characters: http://docs.python.org/library/datetime.html#strftime-strptime-behavior, not the ones in that other table which come from PHP's date() function (yes, this is not ideal that the two are different, but that's the way it is).

%n is not in Python's table -- I think you want %m where you have %n

%j, to Python, is day of the year as a decimal number, not day of the month. I think you want %d where you have %j

%M is minutes, I think you want %b where you have %M

%F is not in the table, I think you want %B where you have %F.

Karen
--
http://tracey.org/kmt/

Reply all
Reply to author
Forward
0 new messages