How can I change the default Django date template format?

DjangoDjango Templates

Django Problem Overview


I have dates in ISO 8601 format in the database, %Y-%m-%d. However, when the date is passed on to the template, it comes out as something like Oct. 16, 2011.

Is there a way that I can manipulate the format to whatever I want?

Django Solutions


Solution 1 - Django

Within your template, you can use Django's date filter. E.g.:

<p>Birthday: {{ birthday|date:"M d, Y" }}</p>

Gives:

> Birthday: Jan 29, 1983

More formatting examples in the date filter docs.

Solution 2 - Django

Solution 3 - Django

Set both DATE_FORMAT and USE_L10N

To make changes for the entire site in Django 1.4.1 add:

DATE_FORMAT = "Y-m-d"

to your settings.py file and edit:

USE_L10N = False

since l10n overrides DATE_FORMAT

This is documented at: https://docs.djangoproject.com/en/dev/ref/settings/#date-format

Solution 4 - Django

Just use this:

{{you_date_field|date:'Y-m-d'}}

This will show something like 2016-10-16. You can use the format as you want.

Solution 5 - Django

You need a date template filter.

E.g:

<td>Joined {{user.date_created|date:"F Y" }}<td>

This returns Joined December 2018.

Solution 6 - Django

In order to change the date format in the views.py file and then assign it to template:

# get the object details 
home = Home.objects.get(home_id=homeid)

# get the start date
_startDate = home.home_startdate.strftime('%m/%d/%Y')

# assign it to template 
return render_to_response('showme.html', {'home_startdate':_startDate},  context_instance=RequestContext(request) )  

Solution 7 - Django

If you need to show a short date and time (11/08/2018 03:23 a.m.), you can do it like this:

{{your_date_field|date:"SHORT_DATE_FORMAT"}} {{your_date_field|time:"h:i a"}}

Details for this tag is here and more about dates according to the given format is here.

Example:

<small class="text-muted">Last updated: {{your_date_field|date:"SHORT_DATE_FORMAT"}} {{your_date_field|time:"h:i a"}}</small>

Solution 8 - Django

{{yourDate|date:'*Prefered Format*'}}

Example:

{{yourDate|date:'F d, Y'}}

For the preferred format: Built-in template tags and filters, date

Solution 9 - Django

If you are getting an error in calling a Date value in a Django template, try this

{{ViewfieldValue.CreateModelValue|date:"M d, Y" }}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbash-View Question on Stackoverflow
Solution 1 - DjangomedmundsView Answer on Stackoverflow
Solution 2 - DjangoIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 3 - DjangoCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 4 - DjangohardcoderrslView Answer on Stackoverflow
Solution 5 - DjangoToluwalemiView Answer on Stackoverflow
Solution 6 - DjangoktaView Answer on Stackoverflow
Solution 7 - DjangonilsovianiView Answer on Stackoverflow
Solution 8 - DjangoMd. Zahangir AlamView Answer on Stackoverflow
Solution 9 - DjangoManoj ParmarView Answer on Stackoverflow