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

  1. UTC-based astimezone filter by miracle2k 3 years, 10 months ago
  2. Template tags for localizing UTC times with pytz by wolever 4 years ago
  3. astimezone template tag by whardier 4 years, 5 months ago
  4. Auto rendering decorator with options by Batiste 7 years, 7 months ago
  5. filter dates to user profile's timezone by Scanner 8 years, 4 months ago

Comments

Please login first before commenting.