Add filter to PartialDate

Viewed 102

I have date collected like PartialDate if date is complete, date is in english format ('yyyy-mm-dd') in my template I would like to convert this date into french format but considering the date can be partial

if only year is known, I would like to see 2019 if only year and month areknown, I would like to see 10/2019 (convert 2019-10 to 10/2019) and if the date is complete, I would like to see 16/10/2019 (convert 2019-10-16 to 16/10/2019)

I tried to add some logic in my template

{% if participante.pat_nai_dat|length_is:"4" %}
    //do something
{% elif participante.pat_nai_dat|length_is:"7" %}
    //do something
{% else %}
    {{ participante.pat_nai_dat|date:"d/m/Y" }}
{% endif %}

but length_is and date filtesr do not work with PartialDate object I've read Django documentation to for more appropriate filter but did not find any my approach is probably wrong... how can I do this?

2 Answers

I resolve my problem using personalized filter

PartialDate has methods that return True/False depending on the precision.

>>> partial_date = PartialDate("2015-11-01")
>>> partial_date.precisionDay()
True
>>> partial_date.precisionMonth()
False
>>> partial_date.precisionYear()
False

Should you want it printed in a format, PartialDate supports that too!

>>> partial_date.format('%Y', '%m/%Y', '%m/%d/%Y')
'11/01/2015'
>>> partial_date = PartialDate("2015-11")
>>> partial_date
2015-11
>>> partial_date.format('%Y', '%m/%Y', '%m/%d/%Y')
'11/2015'

and as such I believe , format would be exactly what you need

I suggest having a look at PyPi's documentation

Related