Login

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

Author:
obeattie
Posted:
April 17, 2007
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.