- Author:
- bmihelac
- Posted:
- March 30, 2009
- Language:
- HTML/template
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
templatetags/date_range.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import re
from django import template
register = template.Library()
@register.simple_tag
def format_date_range(from_date, to_date):
"""
>>> import datetime
>>> format_date_range(datetime.date(2009,1,15), datetime.date(2009,1,20))
'15. - 20.01.2009.'
>>> format_date_range(datetime.date(2009,1,15), datetime.date(2009,2,20))
'15.01. - 20.02.2009.'
>>> format_date_range(datetime.date(2009,1,15), datetime.date(2010,2,20))
'15.01.2009. - 20.02.2010.'
>>> format_date_range(datetime.date(2009,1,15), datetime.date(2010,1,20))
'15.01.2009. - 20.01.2010.'
Use in django templates:
{% load date_range %}
{% format_date_range exhibition.start_on exhibition.end_on %}
"""
from_format = to_format = '%d.%m.%Y.'
if (from_date.year == to_date.year):
from_format = from_format.replace('%Y.', '')
if (from_date.month == to_date.month):
from_format = from_format.replace('%m.', '')
return " - ".join((from_date.strftime(from_format), to_date.strftime(to_format), ))
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
|
More like this
- Bootstrap Accordian by Netplay4 5 years, 9 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 9 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 9 months ago
- Reusable form template with generic view by roldandvg 8 years, 10 months ago
- Pagination Django with Boostrap by guilegarcia 9 years ago
Comments
Try it with same month and different year. Like this:
Is it what you mean? Maybe it will better to put second if inside first if?
#
Astur, you are right - I updated code to reflect this. Thanks
#
Please login first before commenting.