2

I am trying to change the date format from mm/dd/yyyy to dd/mm/yyyy in a DateInput widget. I am trying to change the format the following way.

class Meta:
    model = Patient
    fields = '__all__'
    widgets = {
        'date_of_birth': widgets.DateInput(
            format='%d-%m-%Y',
            attrs={'type': 'date', 'class': 'form-control'})
    }

I noticed that if I don't include the 'type': 'date' attribute the date is interpreted in the correct format, but in that case the widget isn't displayed but just a text-field. So you have to write the date manually (including /). With the DateInput widget that looks like this with dd and mm switched DateInput widget


I have also tried changing the settings.py the following way which also didn't help.

LANGUAGE_CODE = 'en-GB'
TIME_ZONE = 'CET'
USE_L10N = True
USE_TZ = True
DATE_INPUT_FORMATS = ('%d/%m/%Y')

2 Answers 2

0

In settings.py set DATE_INPUT_FORMATS as below:

    DATE_INPUT_FORMATS = ['%d/%m/%Y']

And in your ModelForm you could do something like below:

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

keep in mind, that formats must be defined in the forms.py and not in the models.py.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the format is correct but that will just result in a text field. I am trying to format a DateInput widget with a pop up calendar. When I specify 'type': 'date' the format gets overwritten by the widgets format.
-1

The issue was with my computer settings. I had the language set to English (US). Changing the language to English (UK) fixed the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.