1

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 Answers2

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.

Amun_Re
  • 136
  • 6
  • 1
    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. – SwaggyPP1130 Mar 03 '22 at 09:25
  • I have the same issue. – peterretief Jun 14 '23 at 09:20
-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.