6

In my Django html template, I get my SOLR facet_date result using haystack in the format "2015-01-01T00:00:00Z". How can I parse it in format "01/01/2015" in my template? My template is

{{ facets.dates.created.start }}

What "|date:" option should I add to my template? Thanks!

3 Answers 3

9

If your date is a ISO string instead of a Python datetime.datetime, I guess you will have to parse it on the view or write a custom filter:

# yourapp/templatetags/parse_iso.py
from django.template import Library
import datetime

register = Library()

@register.filter(expects_localtime=True)
def parse_iso(value):
    return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")

Then at the template:

{% load parse_iso %}

{{ value|parse_iso|date:'d/m/Y'}}

[edit]

got this error Exception Type: TemplateSyntaxError at /search/ Exception Value: 'parse_iso' is not a valid tag library: Template library parse_iso not found

Make sure you follow the code layout prescribed in the docs:

yourapp/
    __init__.py
    models.py
    ...
    templatetags/
        __init__.py
        parse_iso.py
    views.py

Your country may use m/d/Y (01/01/2015 is ambiguous, I suggest using an example like 31/01/2015 so it is clear if the first number represents day or month).

3
  • got this error Exception Type: TemplateSyntaxError at /search/ Exception Value: 'parse_iso' is not a valid tag library: Template library parse_iso not found
    – MC X
    Dec 1, 2015 at 4:49
  • Please follow the directory layout described in Django docs: make sure templatetags is a python module inside your app containing a file named __init__.py (may be empty) along with parse_iso.py. Dec 1, 2015 at 4:50
  • Great answer. Though instead of .strptime() to manually formatting the string, it is better to use .fromisoformat().
    – xyres
    Apr 20, 2022 at 16:28
4

If {{ facets.dates.created.start }} is a datetime object then you can use

{{ facets.dates.created.start|date:"SHORT_DATE_FORMAT" }}

In case you are providing a string you can create a template filter to convert the string to datetime object and apply the date filter

@register.filter
def stringformat(value, args):
    return datetime.strptime(value, args)

In the template:

{{ facets.dates.created.start|stringformat:"%Y-%m-%dT%H:%M:%SZ"|date:"SHORT_DATE_FORMAT" }}
-1

You can use Django template tags for this. You need to use {{my_date|date:"some_format"}} which takes "my_date" as the argument (it should be a date object) to "date" tag and then formats it based on the given format.

{{facets.dates.created.start|date:"d/m/Y"}}
2
  • I tried this, but does not work. When I just do {{ facets.dates.created.star }}, 2015-01-01T00:00:00Z will be shown. But when I add date:"d/m/Y", nothing shows when I load the template.
    – MC X
    Dec 1, 2015 at 4:48
  • Please include some text explaining why you think this might solve the OP's problem. Help others to understand.
    – APC
    Dec 1, 2015 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.