[Django]-How to merge a date range in django templates/views

4👍

Something like this should work. It makes sure that the month and year are the same, in which case outputs string like Oct. 12-20, 2012. If not, then it outputs Oct. 12, 2012 - Oct. 22, 2013.

{% if date1|date:"nY" == date2|date:"nY" %} {# same month and day #}
    {{ date1|date:"N j" }}-{{ date2|date:"j, Y" }}
{% else %}
    {{ date1|date:"N j, Y" }} - {{ date2|date:"N j, Y" }}
{% endif %}

Here is a full reference for Django date template filter.

Leave a comment