Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django date format 'dd-mm-yyyy'

Tags:

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 :(

like image 460
Robert Johnstone Avatar asked Feb 02 '11 15:02

Robert Johnstone


People also ask

What is default date format in Django?

django default date to be in format %d-%m-%Y.

How do I get current year in Django?

The full tag to print just the current year is {% now "Y" %} . Note that the Y must be in quotes. For the record, {% now %} won't give proper results for users within different time zone than the server.

How do I change the date on a form?

Press Control+1 or Command+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type. You can adjust this format in the last step below.


1 Answers

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]

like image 146
Robert Johnstone Avatar answered Sep 20 '22 03:09

Robert Johnstone