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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.