Login

date range

Author:
mackenzie_kearl
Posted:
February 16, 2008
Language:
Python
Version:
.96
Tags:
date templatetags
Score:
2 (after 2 ratings)

template tag to format two dates as a date range

 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from django import template

register = template.Library()

class DateRangeNode(template.Node):
    def __init__(self, start_date, end_date, format_string):
        self.start_date = template.Variable(start_date)
	self.end_date = template.Variable(end_date)
	self.format = {}
	if not len(format_string) == 0:
		format_string = format_string.encode('utf-8').strip("\"")
		self.format['day'],self.format['month'],self.format['year'] = format_string.split()
	else:
		self.format['day'],self.format['month'],self.format['year'] = "%d","%m","%Y"
		
    def render(self, context):
        try:
            start_date = self.start_date.resolve(context)
            end_date = self.end_date.resolve(context)
        except template.VariableDoesNotExist:
            return ''
	
	start_format = ""
	end_format = ""
	if start_date.day == end_date.day:
		end_format = self.format['day']
	else:
		start_format = self.format['day']
		end_format = self.format['day']
		
	if start_date.month == end_date.month:
		end_format += " "+self.format['month']
	else:
		start_format += " "+self.format['month']
		end_format += " "+self.format['month']
		
	if start_date.year == end_date.year:
		end_format += " "+self.format['year']
	else:
		start_format += " "+self.format['year']
		end_format += " "+self.format['year']
	print start_format
	print end_format
	
	return start_date.strftime(start_format)+" - "+end_date.strftime(end_format)

def do_date_range(parser, token):
	""" formats two dates as a date range
	
	eg.
	 January 1st 2009 to January 5th 2009 
	would result in:
	 1 - 5 January 2009
	 
	template usage:
	 
	 {% date_range start_date end_date [format string] %}
	 
	 """
	chunks = token.split_contents()
	if not len(chunks) >= 3:
		raise template.TemplateSyntaxError, "%r tag requires two or three arguments" % token.contents.split()[0]
	if not len(chunks) <=4 :
		raise template.TemplateSyntaxError, "%r tag requires two or three arguments" % token.contents.split()[0]
	if len(chunks) == 4:
		format = chunks[3]
	else:
		format = ""
	return DateRangeNode(chunks[1],chunks[2],format)

register.tag('date_range',do_date_range)

More like this

  1. Format date range by bmihelac 6 years, 3 months ago
  2. Group results by a range of dates in admin sidebar with calendar by coolchevy 4 years, 5 months ago
  3. Drop down Datefield by lurker 7 years, 10 months ago
  4. Past days template filter by ramen 5 years, 7 months ago
  5. Get stories for a section based on a date range by donnie-demuth 6 years, 8 months ago

Comments

apdavison (on January 21, 2010):

This is very nice, thank-you. I adapted this to to use Django's date formatting rather than strftime formatting, since it has more options. Basically, I added

from django.utils.dateformat import DateFormat

and then replaced the return statement in render() with

start_df = DateFormat(start_date)
end_df = DateFormat(end_date)
return start_df.format(start_format)+" - "+end_df.format(end_format)

You also have to remove % from the default option.

#

Please login first before commenting.