How to parse "2015-01-01T00:00:00Z" in Django Template?
Asked Answered
M

3

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!

Mars answered 1/12, 2015 at 4:18 Comment(0)
I
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).

Intranuclear answered 1/12, 2015 at 4:40 Comment(3)
got this error Exception Type: TemplateSyntaxError at /search/ Exception Value: 'parse_iso' is not a valid tag library: Template library parse_iso not foundMars
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.Intranuclear
Great answer. Though instead of .strptime() to manually formatting the string, it is better to use .fromisoformat().Encompass
S
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" }}
Shala answered 1/12, 2015 at 4:42 Comment(0)
P
-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"}}
Polyadelphous answered 1/12, 2015 at 4:33 Comment(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.Mars
Please include some text explaining why you think this might solve the OP's problem. Help others to understand.Monophonic

© 2022 - 2024 — McMap. All rights reserved.