Skip to content

Instantly share code, notes, and snippets.

@blackrobot
Created April 10, 2013 19:45
Show Gist options
  • Save blackrobot/5357816 to your computer and use it in GitHub Desktop.
Save blackrobot/5357816 to your computer and use it in GitHub Desktop.
from django import template
register = template.Library()
@register.simple_tag
def date_range(start, end):
""" Takes a start date, and end date, then formats them as a range. """
if end - start >= 365: # More than a year
formats = ('%B %d, %Y', '%B %d, %Y')
elif end.month != start.month: # Same year, different months
formats = ('%B %d', '%B %d, %Y')
else: # Same month and year
formats = ('%B %d', '%d, %Y')
return start.strftime(formats[0]), '-', end.strftime(formats[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment