Login

Template tag for returning 'Morning', 'Afternoon' and 'Evening' in the local timezone

Author:
obeattie
Posted:
April 17, 2007
Language:
Python
Version:
.96
Tags:
template tag templatetag time display
Score:
2 (after 2 ratings)

Returns 'Morning', 'Afternoon', or 'Evening' in the local timezone (specified in settings). Required pytz. 0000-1200 considered morning, 1200-1800 considered afternoon, 1800-0000 considered evening.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@register.simple_tag
def time_of_day():
	import datetime, pytz
	from django.conf import settings
	cur_time = datetime.datetime.now(tz=pytz.timezone(str(settings.TIME_ZONE)))
	if cur_time.hour < 12:
		return 'Morning'
	elif 12 <= cur_time.hour < 18:
		return 'Afternoon'
	else:
		return 'Evening'

More like this

Comments

Please login first before commenting.