Home > Net >  How to properly display the format of date time in Django?
How to properly display the format of date time in Django?

Time:10-05

I want to add the date and time when I uploaded a specific file in my database in Django. In my models.py, I added this line of code:

models.py

date_added = models.DateTimeField(auto_now_add=True)

In the template, I added this code:

{% block content %}
<div >
<tr>
    <td>
        CAR LIST
    </td>
    <td>
        Date Added
    </td>
</tr>

{% for car in cars %}
<tr>
    <td><a href="{% url 'show-car' car.car_id %}">{{ car }}</a></td>
    <td>{{ car.date_added }}</td>
</tr>
{% endfor %}
</div>
{% endblock %}

It correctly displays the date however the time only shows "midnight". How do I format the datetime into Month, Day, Year, Hours, Minutes, Seconds?

CodePudding user response:

Please find the answer in this post.

To summarize, you can format the date in the template using template tags, in you case it would look like this: {{ car.date_added|date:'Y-m-d H:i' }}.

As mentioned in one of the most upvoted answers comments as well you can find the full template tag docs at this Django docs link.

  • Related