Django date format 'dd-mm-yyyy'
Asked Answered
S

8

26

Is there any way I can get django to store my data in postgresql as 'dd-mm-yyyy' (if required) and have the django forms validate for 'dd-mm-yyyy'?

(I have tried without much success things like:

DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = True

And done a lot of googling but no success :(

Subscribe answered 2/2, 2011 at 15:34 Comment(0)
S
47

The problem turned out to be that I needed both the ISO and UK date formats in the settings.py file like so:

DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')

and then the forms.py adjusted to the following:

class ClientDetailsForm(ModelForm):
    date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
    class Meta:
        model = ClientDetails

Why USE_L10N = True can't just do this I don't know!

[Thanks to this and this]

Subscribe answered 3/2, 2011 at 10:23 Comment(3)
You don't have the US format as you said, you've got the UK and ISO formats.Traitor
working fine and date saving to database. but Now date is not rendering to templateAlcina
docs.djangoproject.com/en/3.0/ref/templates/builtins/#dateSubscribe
B
23

DATE_INPUT_FORMATS in settings.py has no effect with USE_I18N = True. This is because django will load specific format for active locale. Django ships with format definitions for many different locales.

You can override django default format definitions:

mysite/
    formats/
        __init__.py
        en/
            __init__.py
            formats.py

As described in django documentation: Creating custom formats file

Bethanie answered 2/2, 2011 at 20:24 Comment(3)
does USE_L10N = True have any effect on DATE_INPUT_FORMATSSubscribe
If USE_L10N = False then django will not use any formats from your settings. USE_L10N = True enables you to set formats in your settings.py. And USE_I18N = True goes even further and enables locale specific formats.Bethanie
Your first sentence (DATE_INPUT_FORMATS in settings.py has no effect with USE_I18N = True.) is the crux of the matter. Just like mentioned in docs, you have to add your DATE_INPUT_FORMATS etc into that formats.py file.Albertype
R
13

In Sevenearths answer there's the comment

Why USE_L10N = True can't just do this I don't know!

The documentation says that USE_L10N = True is the default setting, but to get UK dates to work the LANGUAGE_CODE must also be changed from its default setting of en-us to en-GB.

Once changed, date fields automatically accept the formats "dd/mm/yyyy" and "yyyy-mm-dd" (and possibly others, I've not tested them all) without needing to set input_formats = settings.DATE_INPUT_FORMATS in every form date field.

Railroad answered 16/12, 2015 at 0:21 Comment(0)
A
4

I used below code that worked on Both Add and Edit Form Case.

in settings.py

DATE_INPUT_FORMATS = ('%d/%m/%Y','%d-%m-%Y','%Y-%m-%d')

in forms.py

class MyModelForm(forms.ModelForm):
    issue_date = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=settings.DATE_INPUT_FORMATS)
Alcina answered 30/7, 2020 at 13:10 Comment(0)
S
3

This might not be the answer to the exact question, but since I got here... I would suggest using a date widget, and let Django save the date the way it understands it. Define the field in your form like so:

import datetime
from django import forms

def last_years():
    first_year = datetime.datetime.now().year - 6
    return list(range(first_year + 7, first_year, -1))

class MyForm(forms.Form):
    # This is it... it's user friendly, and can't go wrong parsing it
    date = forms.DateField(widget=forms.SelectDateWidget(years = last_years()))

If you got it into a date field of the Django Model, then the only problem left would be format the output representation. Am I right?

Squinty answered 9/2, 2017 at 21:47 Comment(2)
A lot has changed since I asked this question. I think your answer would be perfect for 1.10+Subscribe
:D indeed... I just bumped on this question and thought I would throw some updated suggestion (6 years later)Squinty
L
2

This works for me

date_of_birth = forms.DateField(
    localize=True,
    widget=forms.DateInput(format = '%Y-%m-%d',attrs={'type': 'date'}),
)
Lifeordeath answered 21/6, 2020 at 1:46 Comment(0)
S
1

You need to set USE_L10N False as shown below to make DATE_INPUT_FORMATS work to change the date format of DateTimeField() and DateField(). *USE_L10N is prior to DATE_INPUT_FORMATS so you need to set USE_L10N False:

DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = False # Here

The doc says below in DATE_INPUT_FORMATS:

When USE_L10N is True, the locale-dictated format has higher precedence and will be applied instead.

Spellman answered 2/6, 2023 at 15:34 Comment(0)
T
0

I would overwrite the DateTimeField put my our logic. It doesn't matter how postgresql is storing it, you can format it how ever you want.

Docs: http://docs.djangoproject.com/en/dev/ref/models/instances/

Tzong answered 2/2, 2011 at 15:53 Comment(2)
so django wont do this for me? I have to do it myself?Subscribe
this might help: docs.djangoproject.com/en/1.1/ref/forms/fields/#datefield to keep it simpleTzong

© 2022 - 2025 — McMap. All rights reserved.