15

I have a datetime object. This is my template file:

<ul>
<li>{{ sale.validity }}</li>
</ul>

and the output i'm getting is in the format:

July 18, 2012, midnight

I would really like to change the output to be numerical in the format: day-month-year so for example the above would be changed to:

18-07-2012

Can someone point me out to the right direction on this?

Tom
  • 9,275
  • 25
  • 89
  • 147
  • Does this answer your question? [How can I change the default Django date template format?](https://stackoverflow.com/questions/7737146/how-can-i-change-the-default-django-date-template-format) – Flimm May 07 '20 at 13:39

1 Answers1

36

Per Django docs, you can use a date filter to format date:

{{ sale.validity|date:"d-m-Y"}} 

Additionally, you can also set DATE_FORMAT (or DATETIME_FORMAT) setting to set a project-wide default for displaying such values if USE_L10N is False. If it's true, you'll want to have a look at this documentation on format localization.

Flimm
  • 136,138
  • 45
  • 251
  • 267
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60