Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{% block content %}
<div class="container">
<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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading