jayt
jayt

Reputation: 768

Django - Change date/time format within HTML tag

I have a Django model that records when a new object from that model is created by a user. This date is printed within my view with the format like... March 20, 2017, 1:49 p.m.

Models.py:

class Model(models.Model):
    title = models.CharField(max_length=50, verbose_name='Title')
    dt_created = models.DateTimeField(auto_now_add=True, verbose_name='Date Created')
    dt_updated = models.DateTimeField(auto_now=True, verbose_name='Date Updated')

However, I want the date to be printed in the format DD/MM/YYYY or MM/DD/YYYY like 20/03/2017 or 03/20/2017 for example.

This is in my HTML as

<h5 class='dt-class'>Created <span class='dt-timestamp'>{{ model.dt_created }}</span></h5>

Is there any way to convert the format of the date/time from within the {{ model.dt_created }} tag? Or must I go into the view to change how this is printed?

Thanks.

Upvotes: 2

Views: 1491

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37364

Use the built in date filter, eg:

{{ model.dt_created|date:"SHORT_DATE_FORMAT" }}

Upvotes: 2

Related Questions